I'm working on creating a custom connector in PowerApps which needs to call an API. The API needs an authorization token passed. Sending the request via Postman, this is set as follows:
Authorization: Bearer 2b3fdha04a4d89aad9c263d5d716bcc379aff0008
When I try to do the same thing via Swagger, the header gets sent like this:
Access-Control-Request-Headers: Authorization
This is how I'm trying to pass the token:
- {name: Authorization, in: header, description: This is the API key, required: true, type: string}
And this is my complete Swagger definition file:
`
swagger: '2.0'
info: {version: 1.0.0, title: ACME Corp, description: ACME sample API}
host: acmeapis.com
basePath: /
schemes: [https]
consumes: [multipart/form-data]
produces: [application/json]
securityDefinitions: {}
paths:
/v4_server/external/v1/authenticate:
post:
summary: Authorize
description: Used to get an authorization token
operationId: Authorize
parameters:
- {in: formData, name: api_key, type: string, description: The API key., required: true}
- {in: formData, name: client_db, type: string, description: The database to use.,
required: true}
- {in: formData, name: username, type: string, description: The username, required: true}
responses:
default:
description: All is well
schema:
type: object
properties:
access_token: {type: string, description: access_token}
expires_in: {type: integer, format: int32, description: expires_in}
refresh_token: {type: string, description: refresh_token}
/v4_server/external/v1/maintenance/resources/properties:
get:
summary: Building/Property Location
- Bearer: []
description: Building/Property Location
operationId: Building_propertyLocation
parameters:
- {name: property_use_id, default: '2', in: query, type: string, required: true}
- {name: $orderBy, default: unit, in: query, type: string, required: true}
- {name: Authorization, in: header, description: This is the API key, required: true, type: string}
responses:
'200': {description: Will send `Authenticated`}
'403': {description: 'You do not have necessary permissions for the resource,'}
default:
description: default
schema: {}
/: {}
definitions: {}
parameters: {}
responses: {}
security: []
tags: []
`
I can't find any examples on how to do this. Can someone point me the way.
Tks
Add the following lines under the schema
securityDefinitions:
Bearer:
type: apiKey
name: Authorization
in: header
description: >-
Enter the token with the `Bearer: ` prefix, e.g. "Bearer abcde12345".
Here is your full swagger (I've tested it on swagger editor - just copy and past into it to see the result)
swagger: '2.0'
info: {version: 1.0.0, title: ACME Corp, description: ACME sample API}
host: acmeapis.com
basePath: /
schemes: [https]
securityDefinitions:
Bearer:
type: apiKey
name: Authorization
in: header
description: >-
Enter the token with the `Bearer: ` prefix, e.g. "Bearer abcde12345".
consumes: [multipart/form-data]
produces: [application/json]
paths:
/v4_server/external/v1/authenticate:
post:
summary: Authorize
description: Used to get an authorization token
operationId: Authorize
parameters:
- {in: formData, name: api_key, type: string, description: The API key., required: true}
- {in: formData, name: client_db, type: string, description: The database to use.,
required: true}
- {in: formData, name: username, type: string, description: The username, required: true}
responses:
default:
description: All is well
schema:
type: object
properties:
access_token: {type: string, description: access_token}
expires_in: {type: integer, format: int32, description: expires_in}
refresh_token: {type: string, description: refresh_token}
/v4_server/external/v1/maintenance/resources/properties:
get:
summary: Building/Property Location
description: Building/Property Location
operationId: Building_propertyLocation
parameters:
- {name: property_use_id, default: '2', in: query, type: string, required: true}
- {name: $orderBy, default: unit, in: query, type: string, required: true}
- {name: Authorization, in: header, description: This is the API key, required: true, type: string}
responses:
'200': {description: Will send `Authenticated`}
'403': {description: 'You do not have necessary permissions for the resource,'}
default:
description: default
schema: {}
/: {}
definitions: {}
parameters: {}
responses: {}
security: []
tags: []
Related
In my Swagger documentation, I have to use same pattern multiple time. Currently, it is placed duplicated.
One thing I improved is that making common parameter (by declaring it inside components). But couldn't find anything to avoid this duplicate pattern between parameters and schemas.
Is there any way, I can use declare this pattern once and re-use it wherever it's needed?
My Swagger looks something like:
openapi: 3.0.3
info:
title: My System
description: Self contained system
version: 1.0.0
servers:
- url: /a/{aId}/
description: Contains the partition
security:
- bearerAuth: []
paths:
"/x/{xId}/y/{yId}/z/{zId}":
get:
x-horizon-permission: "geo.floorplan.read"
tags:
- myTag
operationId: getValue
description: Get Value
parameters:
- name: xId
in: path
required: true
schema:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
- name: yId
in: path
required: true
schema:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
- name: zId
in: path
required: true
schema:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
responses:
"200":
description: OK
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Response'
patch:
tags:
- myTag
operationId: Update
description: Update Data
parameters:
- name: xId
in: path
required: true
schema:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
- name: yId
in: path
required: true
schema:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
- name: zId
in: path
required: true
schema:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
requestBody:
required: true
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Request'
responses:
"200":
description: OK
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Response'
components:
schemas:
Request:
type: object
properties:
data:
type: object
properties:
type:
type: string
id:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
Response:
type: object
properties:
links:
type: object
properties:
self:
type: string
data:
$ref: '#/components/schemas/Data'
Data:
type: object
properties:
type:
type: string
id:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
GeometryError:
type: object
required:
- code
- status
- title
properties:
id:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
status:
type: string
example: 400
title:
type: string
detail:
type: string
meta:
type: object
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
Define a schema with this pattern:
components:
schema:
UUID:
type: string
format: uuid # for tools/codegens that have built-in support for UUIDs
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
Then you can reference this schema from other schemas and parameter definitions:
parameters:
- name: xId
in: path
required: true
schema:
$ref: '#/components/schemas/UUID' # <-----
- name: yId
in: path
required: true
schema:
$ref: '#/components/schemas/UUID' # <-----
...
schemas:
Request:
type: object
properties:
data:
type: object
properties:
type:
type: string
id:
$ref: '#/components/schemas/UUID' # <-----
I am trying to use Swagger. Below is the swagger.yml file.
swagger: "2.0"
basePath: /myapp/api
info:
description: My description
title: My title
version: 0.1.0
produces:
- application/json
consumes:
- application/json
schemes:
- http
paths:
/contract:
get:
operationId: "Get contract"
description: Get information
parameters:
- in: path
name: contractId
description: ID
required: true
schema:
type: integer
responses:
200:
description: Information...
schema:
$ref: "#/definitions/contract"
404:
description: "Not found."
post:
operationId: "Create"
parameters:
- in: body
name: contractId
schema:
$ref: '#/definitions/requestBodies/contract'
responses:
200:
description: Success...
400:
description: Problem...
definitions:
contract:
title: Contract
type: object
properties:
name:
title: Name
type: string
services:
title: Services
type: array
items:
title: Service
$ref: '#/definitions/service'
xyz:
title: Xyz
$ref: '#/definitions/virtualMachine'
service:
title: Service
type: object
properties:
containerName:
title: ContainerName
type: string
...
contracts:
title: Contracts
type: array
items:
title: Contract
$ref: '#/definitions/contract'
xyz:
title: Xyz
type: object
properties:
serverId:
title: ServerID
type: string
contractId:
title: ContractID
type: uuid
...
requestBodies:
contract:
content:
application/json:
schema:
$ref: '#/definitions/contract'
When I try to generate the documentation, I get the following error:
swagger generate server -f swagger.yml
2020/10/26 15:43:31 validating spec /home/dalton/workspace/.../.../swagger.yml
The swagger spec at "/home/dalton/workspace/.../.../swagger.yml" is invalid against swagger specification 2.0. see errors :
- definitions.requestBodies.contract in body is a forbidden property
- "definitions.xyz.properties.contractId.type" must validate at least one schema (anyOf)
- definitions.xyz.properties.contractId.type in body must be of type array: "string"
What am I doing wrong?
To make this code pass in the Swagger validation, I had to:
Remove the schema in the contractId parameter (get method);
Remove the requestBodies definition and change the schema in the contract parameter (post method);
Change the type of the contractId in the Xyz definition (the uuid type is not supported by the version 2 of Swagger).
The fixed code is below:
swagger: "2.0"
basePath: /myapp/api
info:
description: My description
title: My title
version: 0.1.0
produces:
- application/json
consumes:
- application/json
schemes:
- http
paths:
/contract:
get:
operationId: "Get contract"
description: Get information
parameters:
- in: path
name: contractId
description: ID
required: true
type: integer
responses:
200:
description: Information...
schema:
$ref: "#/definitions/contract"
404:
description: "Not found."
post:
operationId: "Create"
parameters:
- in: body
name: contractId
schema:
$ref: '#/definitions/contract'
responses:
200:
description: Success...
400:
description: Problem...
definitions:
contract:
title: Contract
type: object
properties:
name:
title: Name
type: string
services:
title: Services
type: array
items:
title: Service
$ref: '#/definitions/service'
xyz:
title: Xyz
$ref: '#/definitions/virtualMachine'
service:
title: Service
type: object
properties:
containerName:
title: ContainerName
type: string
...
contracts:
title: Contracts
type: array
items:
title: Contract
$ref: '#/definitions/contract'
xyz:
title: Xyz
type: object
properties:
serverId:
title: ServerID
type: string
contractId:
title: ContractID
type: string
format: uuid
Maybe you can try to edit your swagger.yml in the online Swagger Editor.
For the Client this one is inside of client.yaml
/clients:
get:
tags:
- "Clients"
description: "List Clients The list capability"
produces:
- "application/json"
parameters:
- name: "tenantIdentifier"
in: "query"
required: true
type: "array"
items:
type: "string"
enum:
- "default"
responses:
200:
description: "successful operation"
400:
description: "Invalid status value"
security:
- basicAuth: []
post:
tags:
- "Clients"
summary: "Create client if address is enabled"
description: ""
operationId: "addClient"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- name: "tenantIdentifier"
in: "query"
description: ""
required: true
type: "array"
items:
type: "string"
enum:
- "default"
- in: "body"
name: "body"
description: "Add what do you wnat to add "
required: true
schema:
allOf:
- $ref: '#/definitions/ClientStructure1'
- $ref: '#/definitions/ClientStructure2'
- $ref: '#/definitions/ClientStructure3'
responses:
405:
description: "Invalid input"
security:
- basicAuth: []
For the USER this is inside of user.yaml
/users:
get:
tags:
- "Users"
summary: "Retrieve list of users"
produces:
- "application/json"
parameters:
- name: "tenantIdentifier"
in: "query"
required: true
type: "array"
items:
type: "string"
enum:
- "default"
responses:
200:
description: "successful operation"
400:
description: "Invalid status value"
security:
- basicAuth: []
post:
tags:
- "Users"
summary: "Adds new application user."
description: "Note: Password information is not required (or processed). Password details at present are auto-generated and then sent to the email account given (which is why it can take a few seconds to complete)."
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- name: "tenantIdentifier"
in: "query"
description: ""
required: true
type: "array"
items:
type: "string"
enum:
- "default"
- in: "body"
name: "body"
description: "Mandatory Fields: username, firstname, lastname, email, officeId, roles, sendPasswordToEmail"
required: true
schema:
$ref: "#/definitions/StructureForCreateUSer"
responses:
400:
description: ""
404:
description: ""
security:
- basicAuth: []
You can't $ref whole paths, but you can $ref the contents of individual paths. In your example, you can use:
paths:
/clients:
$ref: clients.yaml#/~1clients
/users:
$ref: users.yaml#/~1users
clients.yaml#/~1clients means we take the clients.yaml file, then read the contents of the /clients node in that file and substitute the $ref with that contents. ~1clients is /clients with / escaped as ~1 according to JSON Pointer rules.
To simplify the references, you can remove the /clients: and /users: nodes from your clients.yaml and users.yaml files
# clients.yaml
get:
description: "List Clients The list capability"
...
post:
summary: "Create client if address is enabled"
...
and then use
paths:
/clients:
$ref: clients.yaml
/users:
$ref: users.yaml
OpenAPI 3 allows the use of the $ref keyword:
https://swagger.io/docs/specification/using-ref/
I'm fairly new to swagger and would appreciate some direction on good resources for learning.
I have a test project that I'm putting together and I am running into some issues.
I'm getting an error saying that the "parameters" in my "delete" block is not valid. It looks ok to me from what I've seen in the examples. But apparently I'm missing something. Any ideas?
swagger: "2.0"
info:
version: "2"
title: My Title
description: Provides services for vacation rentals site
termsOfService: Private
contact:
name: My Name
url: www.myurl.com
email: my#email.address
license:
name: MIT
url: http://opensource.org/licenses/MIT
schemes:
- http
host: myurl.com
basePath: /api
paths:
/guestbook:
get:
summary: Gets some persons
description: Returns a list containing all persons.
responses:
200:
description: A list of posts
schema:
type: array
items:
required:
- firstName
properties:
firstName:
type: string
lastName:
type: string
email:
type: string
comment:
type: string
post:
summary: Adds a comment to the guestbook
description: Adds a comment to the guestbook
parameters:
- name: firstname
in: formData
required: true
type: string
- name: lastname
in: formData
required: true
type: string
- name: email
in: formData
required: true
type: string
- name: comment
in: formData
required: true
type: string
responses:
201:
description: Shows a successful post
'405':
description: Invalid input
/guestbook/{id}:
get:
summary: Gets a single post
description: Returns a single post
operationId: getPost
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
type: integer
format: int64
responses:
200:
description: A list of posts
schema:
type: array
items:
required:
- firstName
properties:
id:
type: number
firstName:
type: string
lastName:
type: string
email:
type: string
comment:
type: string
delete:
summary: Removes a post
description: Removes a post
operationId: deletePost
parameters:
- name: id
in: path
responses:
200:
description: Post has been removed
You just need to describe the id parameter in the delete /guestbook/{id} operation just like you did in get /guestbook/{id}.
delete:
summary: Removes a post
description: Removes a post
operationId: deletePost
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
type: integer
format: int64
responses:
200:
description: Post has been removed
You can also define this parameter once for all operations of path /guestbook/{id}:
/guestbook/{id}:
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
type: integer
format: int64
get:
summary: Gets a single post
description: Returns a single post
operationId: getPost
responses:
200:
description: A list of posts
schema:
type: array
items:
required:
- firstName
properties:
id:
type: number
firstName:
type: string
lastName:
type: string
email:
type: string
comment:
type: string
delete:
summary: Removes a post
description: Removes a post
operationId: deletePost
responses:
200:
description: Post has been removed
If you need to learn how to write OpenAPI (fka. Swagger) specification files, you can read my Writing OpenAPI/Swagger Specification Tutorial
Working on writing an API spec for my service with swagger. I'm using a model definition ('#/definitions/prototype') as the body parameter of both the POST /prototypes and PATCH /prototypes/:id routes.
How do you specify that the PATCH route only accepts a subset of the properties in the body of the request that the POST route does? For example, I want the PATCH /instances/:id route to only allow modification of the mobileDeviceId prototypes property.
swagger: "2.0"
info:
title: ""
description: ""
version: "1.0.0"
host: foo.example.com
schemes:
- https
basePath: /v1
produces:
- application/json
consumes:
- application/json
paths:
/prototypes:
post:
summary: Create new prototype
parameters:
- name: prototype
in: body
description: Prototype object
required: true
schema:
$ref: "#/definitions/Prototype"
responses:
201:
description: Success
schema:
$ref: "#/definitions/SuccessCreated"
403:
description: Prototype limit exceeded error
schema:
$ref: "#/definitions/Error"
default:
description: Unexpected error
schema:
$ref: "#/definitions/Error"
/prototypes/{id}:
patch:
summary: Update an existing prototype's properties
parameters:
- name: id
in: path
type: number
description: ID property of a prototype
required: true
- name: prototype
in: body
description: Prototype object
required: true
schema:
$ref: "#/definitions/Prototype"
responses:
200:
description: Success
definitions:
Prototype:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
mobileDeviceId:
type: number
SuccessCreated:
type: object
description: Returned as response to successful resource create request
properties:
code:
type: number
default: 201
message:
type: string
Error:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
fields:
type: string
Swagger uses json-schema: http://json-schema.org
$refs provide you w/ a way to repeat existing json-schema at a new path.
Notice that you are using a $ref for patch/parameters/-name:prototype/schema.
You can create a new definition just for patch in the definitions section and reference it instead
swagger: "2.0"
info:
title: ""
description: ""
version: "1.0.0"
host: foo.example.com
schemes:
- https
basePath: /v1
produces:
- application/json
consumes:
- application/json
paths:
/prototypes:
post:
summary: Create new prototype
parameters:
- name: prototype
in: body
description: Prototype object
required: true
schema:
$ref: "#/definitions/Prototype"
responses:
201:
description: Success
schema:
$ref: "#/definitions/SuccessCreated"
403:
description: Prototype limit exceeded error
schema:
$ref: "#/definitions/Error"
default:
description: Unexpected error
schema:
$ref: "#/definitions/Error"
/prototypes/{id}:
patch:
summary: Update an existing prototype's properties
parameters:
- name: id
in: path
type: number
description: ID property of a prototype
required: true
- name: prototype
in: body
description: Prototype object
required: true
schema:
$ref: "#/definitions/PatchPrototype"
responses:
200:
description: Success
definitions:
Prototype:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
mobileDeviceId:
type: number
PatchPrototype:
type: object
properties:
mobileDeviceId:
type: number
SuccessCreated:
type: object
description: Returned as response to successful resource create request
properties:
code:
type: number
default: 201
message:
type: string
Error:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
fields:
type: string