swagger api documentation issues with nested objects - swagger

I want to use swagger to document our internal Jenkins Jobs and parameters. So that everybody knows what the body has to look like to properly trigger a Jenkins jobs over the API.
I am writing the API documentation with a swagger.yml file. Where I am totally struggling is to document nested objects.
Jenkins needs the parameters in JSON. The curl request looks like this
curl --request POST \
--url https://myjenkins.com/job/demojob/build \
--form 'json={
"parameter": [
{
"name": "FilePath",
"value": "E:\\Jenkins"
},
{
"name": "FileName",
"value": "JenkinsAPI.txt"
},
{
"name": "FileContent",
"value": "I am a file created by jenkins through API"
},
{
"name": "Computer",
"value": "myhost"
}
]
}'
I am able to create a yml file that contains something like this, and that does not resemble what I need at all.
Can somebody point me in the right direction, or give me an example?
paths:
/job/demojob/build:
post:
summary: triggers the job
parameters:
- name: parameters
in: body
required: true
- name: filePath
in: body
schema:
type: string
default: "C:\\fancyfolder"

Please check below mentioned yml file with swagger properties mapping and you can change ApiResponse according to your requirement. :
openapi: 3.0.1
info:
title: Swagger Jenkins
description: 'This is a sample server Jenkins server. You can find out more about Swagger'
version: 1.0.0
servers:
- url: https://jenkins.com
tags:
- name: build
description: Everything about your build
paths:
/job/demojob/build:
post:
tags:
- build
summary: build the given branch
operationId: build
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/RequestParameters'
responses:
200:
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ApiResponse'
components:
schemas:
RequestParameters:
type: array
items:
$ref: '#/components/schemas/Parameters'
Parameters:
type: object
properties:
name:
type: string
value:
type: string
description: Order Status
ApiResponse:
type: object
properties:
code:
type: integer
format: int32
type:
type: string
message:
type: string

Related

Ansible GITEA server , API call to create multiple repos for 2 users, 1 for Superadmin gitea account and the other for the git standard account

Hi everyone and thanks in advance for your support, I wanna create 1 API call to create 2 repos, 1 assigned to a Superadmin gitea account and the other for standard gitea user named gitea, I assume this would need to be with a list of users in variables and a list of repos in variables also using with_items but I don't seem to get it, down I'm attaching my API call format in Ansible to achieve 1 repo/1user, please someone help:
- name: "gitea - Create repo"
uri:
url: '{{ gitea_api_url }}/user/repos'
method: POST
body_format: json
status_code: 201
headers:
Authorization: token {{ saved_tokens if token_ is undefined else token_ }}
body:
auto_init: true
default_branch: "main"
description: "hi"
gitignores: ""
issue_labels: ""
license: ""
name: "{{ item }}"
private: true
readme: ""
template: true
trust_model: "default"
owner: '{{ gitea.users }}'
with_items: "{{ repo_names }}"
register: _repostatus
I only assume it could work with a list of 2 users and a list of 2 repo names using somehow with_items and conditionals together, in this case I have the following:
gitea:
users:
- name: '{{ gitea_admin_account }}'
admin: true
- name: '{{ gitea_standard_account }}'
admin: false
repo_names:
- test2
- test3
How to use the 'admin' flag to create 'test2' to the admin and 'test3' to the standard user?
I cannot see an owner key in the Gitea API for [POST] /user/repos, only in the 201 response.
If you want to add other users besides the repository owner, you should probably use [PUT] /repos/{owner}/{repo}/collaborators/{collaborator} or similar after creating the repository.
For this request you can loop by using loop keyword:
- name: "gitea - Add collaborators to total repo_list"
uri:
url: '{{ gitea_api_url }}/repos/{{ repo_owner }}/{{ item.0 }}/collaborators/{{ item.1.name }}'
method: POST
body_format: json
status_code: 204
headers:
Authorization: token {{ saved_tokens if token_ is undefined else token_ }}
body:
permission: "{{ 'admin_permission_str' if item.1.admin else 'regular_permission_str' }}"
loop: "{{ repo_names | product(gitea.users) }}"
register: _repostatus
To iterate over two lists you have to create a cartesian product of them to bring both into one data structure. Then you can iterate over this one data structure. The { repo_names | product(gitea.users) }} results in the following data structure:
[
[
"test2",
{
"admin": true,
"name": "gitea_admin_account"
}
],
[
"test2",
{
"admin": false,
"name": "gitea_standard_account"
}
],
[
"test3",
{
"admin": true,
"name": "gitea_admin_account"
}
],
[
"test3",
{
"admin": false,
"name": "gitea_standard_account"
}
]
]
You can use .0 and .1 to address the repository name or the users dict, respectively.
Alternatively you can work with with_nested/with_cartesian instead of using loop directly, you can find more about this in the Ansible Docs. But this is not necessary if you use loop as described above.

Openapi Springfox 3.0 examples

I have a yaml file which has examples for request body. but swagger-ui springfox generated json file generated by springfox doesn't show the request body example, which makes it to show schema example.
Any workaround for displaying request body examples?
#openapi #swagger-ui #springfox
openapi: 3.0.2
info:
title: Rule APIs
description: Rule APi
version: 1.0.0
contact:
email: abc#gmail.com
servers:
- url: 'https://{server}/v1/rule'
variables:
server:
default: localhost
security:
- BasicAuth: []
paths:
#####################################################################################################
############################ Rule APIs ###############################
#####################################################################################################
/rule-set:
parameters:
- $ref: '#/components/parameters/XRequestIdHeader'
post:
summary: Create a rule set.
description: Create a rule set.
operationId: createRule
tags:
- Rule
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/RuleSet'
example:
name: My RuleSet
description: A new ruleSet
state: enabled
condition:
conditionType: ConditionAttributes
isNegate: false
dictionaryName: DEVICE
attributeName: attribute
operator: equals
attributeValue: attribute
serviceName: service
isProxy: false
responses:
'201':
$ref: '#/components/responses/BadRequest'
'400':
$ref: '#/components/responses/BadRequest'
json output

How to access AWS SAM Mapping in swagger.yaml / openapi.yaml files

I have declared a mapping named StageMap in my sam.yaml file:
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Parameters:
ProjectName:
Type: String
SubProjectName:
Type: String
Stage:
Type: String
AllowedValues:
- dev
- test
- preprod
- prod
...
Mappings:
StageMap:
dev:
AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-dev-AuthorizerFunction-1RR2YJ5STBUB6/invocations
test:
AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-test-AuthorizerFunction-UQ1EQ2SP5W6G/invocations
preprod:
AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-preprod-AuthorizerFunction-UQ1W6EQ2SP5G/invocations
prod:
AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-prod-AuthorizerFunction-5STBUB61RR2YJ/invocations
I would like to use this mapping in my swagger.yaml I have tried the following:
...
x-amazon-apigateway-authorizer:
type: request
authorizerUri:
Fn::FindInMap:
- 'StageMap'
- Ref: 'Stage'
- 'AuthorizerArn
I also tried this solution but I got an error Every Mappings attribute must be a String or a List.
Can you please let me know how to access one of the values in the mapping in the swagger.yaml? Thanks!
I found the following in the AWS SAM docs:
You cannot include parameters, pseudo parameters, or intrinsic functions in the Mappings section.
So I changed:
AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-dev-AuthorizerFunction-1RR2YJ5STBUB6/invocations
For:
AuthorizerFunctionName: auth-bk-main-dev-AuthorizerFunction-1RR2YJ5STBUB6
And in the swagger.yaml I used the following:
x-amazon-apigateway-authorizer:
type: request
authorizerUri:
Fn::Sub:
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AuthorizerFunctionName}/invocations
- AuthorizerFunctionName:
Fn::FindInMap:
- 'StageMap'
- Ref: 'Stage'
- 'AuthorizerFunctionName'

Swagger Codegen for OAS3

I'm trying to use Swagger Codegen for my OAS3 project.
I have cloned swagger-codegen branch 3.0.0. and when I run the generate command I get the following error:
[main] ERROR io.swagger.parser.SwaggerCompatConverter - failed to read resource listing
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'openapi': was expecting ('true', 'false' or 'null')
at [Source: (String)"openapi: 3.0.0
Does Codegen supports openapi 3.0.1 yet? If yes what am I missing ?
UPDATE - Yaml file and codegen command
Petstore.yaml file :
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Pets:
type: array
items:
$ref: "#/components/schemas/Pet"
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
Codegen command:
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
java -jar ./swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i "./definitions/petstoreV3.yaml" -l javascript -o "$SCRIPT_DIR/.swagger_gen_temp"
The error occurs because you are running V2 codegen (from the master branch) instead of V3 codegen (from the 3.0.0 branch). V2 codegen does not support OpenAPI 3.0.
You can download the JAR of V3 codegen from:
http://central.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.11/swagger-codegen-cli-3.0.11.jar
V3 codegen, however, does not currently have a JavaScript client generator.

windows heat template user_data can not work

I am new to CloudbaseInit. I have setup a image with CloudbaseInit,and can build the machine with new password and expand hdd size all ok (using command :nova boot). But I want to use heat stack-create with a heat template file
heat_template_version: 2013-05-23
description: dtb test hottest,for test add parameters.
parameters:
flavor:
type: string
label: paasflavor
description: paasflavor flavor to be used
default: c1m2h90
availability_zone:
type: string
description: The Availability Zone to launch the instance.
default: nova
name:
type: string
description: name of host.
resources:
server1_port1:
type: OS::Neutron::Port
properties:
network_id: 70c1faf0-51f6-4cb9-b324-7bc2cc6fab5b
server1:
type: OS::Nova::Server
properties:
name: { get_param: name }
image: template_win2008
flavor: { get_param: flavor }
availability_zone: { get_param: availability_zone }
networks:
- port: { get_resource: server1_port1 }
user_data:
echo 11 > \"c:\\yp\\333"\n,
outputs:
server1_ip:
description: Private IP address of server1
value: { get_attr: [ server1, first_address ] }
Machine builds ok. When I login into it, and go to c:\yp, I find nothing. I think user_data is wrong, and command not work. I have tried several other ways for writing user_data part, but all failed. I never build windows machine with heat template.
Add pipe(|) after user_data:
user_data: |

Resources