Set Request Validator of API Gateway by serverless - swagger

I want to set Request Validator of API Gateway by serverless. I tried two different settings for the Request Validator. But, both methods have failed. I have summarized what I did, so please let me know if there is something wrong.
I write the API specification in swagger(OAS3.0). Therefore I tried to realize the setting of Request Validator using OAS extension. After I did sls deploy using below swagger.yaml and serverless.yml, None of the validate patterns described in x-amazon-apigateway-request-validators were added to the Request Validator options.
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-request-validator.html
swagger.yaml is below:
openapi: 3.0.0
info:
description: xxx
version: '0.1'
title: xxx API
x-amazon-apigateway-request-validators:
body-only:
validateRequestBody: true,
validateRequestParameters: false
except-body:
validateRequestBody: false,
validateRequestParameters: true
all:
validateRequestBody: true,
validateRequestParameters: true
tags:
- name: auth
description: xxx
paths:
/login:
post:
tags:
- auth
summary: xxx
description: ''
x-amazon-apigateway-request-validator: all
responses:
'200':
description: success
content:
application/json:
schema:
$ref: '#/components/schemas/AuthResponse'
'400':
description: fail
content:
application/json
'401':
description: fail
content:
application/json
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/AuthRequest'
required: true
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
uri: "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:xxx-api-dev-login/invocations"
passthroughBehavior: "when_no_match"
httpMethod: "POST"
contentHandling: "CONVERT_TO_TEXT"
type: "aws_proxy"
My serverless.yml is below:
resources:
Resources:
RestApi :
Type : AWS::ApiGateway::RestApi
Properties :
Body : ${file(./swagger.yaml)}
LoginApiToInvokeLambda:
Type: AWS::Lambda::Permission
DependsOn: LoginLambdaFunction
Properties:
FunctionName: xxx-ext-api-dev-login
Action: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
I tried to realize the setting of Request Validator using AWS::ApiGateway::RequestValidator. After I did sls deploy using above swagger.yaml and below serverless.yml, the all described in RequestValidatorAll in severless.yml were added to the Request Validator options. But the default value of Request Validator was still NONE.
resources:
Resources:
RestApi :
Type : AWS::ApiGateway::RestApi
Properties :
Body : ${file(./swagger.yaml)}
LoginApiToInvokeLambda:
Type: AWS::Lambda::Permission
DependsOn: LoginLambdaFunction
Properties:
FunctionName: xxx-ext-api-dev-login
Action: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
RequestValidatorAll:
Type: AWS::ApiGateway::RequestValidator
Properties:
Name: all
RestApiId:
Ref: RestApi
ValidateRequestBody: true
ValidateRequestParameters: true

You need to remove the commas in your YAML.
i.e
x-amazon-apigateway-request-validators:
body-only:
validateRequestBody: true,
validateRequestParameters: false
except-body:
validateRequestBody: false,
validateRequestParameters: true
all:
validateRequestBody: true,
validateRequestParameters: true
should be
x-amazon-apigateway-request-validators:
body-only:
validateRequestBody: true
validateRequestParameters: false
except-body:
validateRequestBody: false
validateRequestParameters: true
all:
validateRequestBody: true
validateRequestParameters: true
Once you do this the YAML will be valid and it should work.

Related

how to add content type in Swagger yaml file?

I tried CRUD Operation in Swagger api using Node js. I tried PUT method using Swagger api, it's Successfully updated in db but it's throwing content type error. I tried many ways. I added application/json, application/xml, text/plain but it's still throwing same error. How to fix it can give me any solution.
Swagger.yaml
swagger: "2.0"
info:
version: "0.0.1"
title: Movie DB
# during dev, should point to your local machine
host: localhost:8000
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
securityDefinitions:
Bearer:
type: apiKey
name: Authorization
in: header
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/movies:
# binds a127 app logic to a route
x-swagger-router-controller: movies
post:
description: Creates a new movie entry
operationId: create
security:
- Bearer: []
parameters:
- name: movie
required: true
in: body
description: a new movie details
schema:
$ref: "#/definitions/MovieBody"
responses:
"200":
description: a successfully stored movie details
schema:
$ref: "#/definitions/MovieBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/movies/{id}:
x-swagger-router-controller: movies
get:
description: get movie
operationId: show
security:
- Bearer: []
parameters:
- name: id
required: true
in: path
description: get particular movie details
type: string
responses:
"200":
description: Sucess
schema:
$ref: "#/definitions/MovieBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
put:
produces:
- '*/*'
description: Update Movie
operationId: update
security:
- Bearer: []
parameters:
- name: id
required: true
in: path
type: string
- name: movie
required: true
in: body
description: an updated movie details
schema:
$ref: "#/definitions/MovieBody"
responses:
"200":
description: Sucess
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
delete:
description: Delete Single Record
operationId: deleted
security:
- Bearer: []
parameters:
- name: id
required: true
in: path
description: remove single record in db
type: string
responses:
"200":
description: Sucess
schema:
$ref: "#/definitions/MovieBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/login:
x-swagger-router-controller: movies
post:
description: Get Jwt Authentication Token
operationId: login
produces:
- application/json
- application/xml
parameters:
- name: Userdetails
required: true
in: body
description: Jwt Auth token
schema:
$ref: "#/definitions/LoginBody"
responses:
"200":
description: Sucess
schema:
$ref: "#/definitions/LoginBody"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/get:
x-swagger-router-controller: movies
get:
description: Get all Data
operationId: get
security:
- Bearer: []
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/MovieListBody"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
definitions:
MovieListBody:
required:
- id
- movies
properties:
id:
type: integer
movies:
type: array
items:
$ref: "#/definitions/Movie"
Movie:
required:
- title
- gener
- year
properties:
title:
type: string
gener:
type: string
year:
type: integer
Login:
required:
- id
- name
- company
properties:
id:
type: integer
name:
type: string
company:
type: string
MovieBody:
required:
- movies
properties:
movies:
$ref: "#/definitions/Movie"
LoginBody:
required:
- details
properties:
details:
$ref: "#/definitions/Login"
ErrorResponse:
required:
- message
properties:
message:
type: string
Facing Issue
Error: Response validation failed: invalid content type (text/html). These are valid: application/json, */*

How to create a POJO class from OpenAPI (Swagger)?

How can I create a POJO class for the following OpenAPI (Swagger) definition?
paths:
/execute:
post:
summary: 'Execute the web service and get a response synchronously'
operationId: execute
parameters:
- in: body
name: 'body'
description: 'Execution request'
schema:
$ref: '#/definitions/ExecutionRequest'
- in: query
name: 'api-version'
description: 'the version of the backend API'
required: true
type: number
default: 2.0
- in: query
name: 'format'
description: 'the format of the backend API'
required: true
type: string
default: "swagger"
responses:
'200':
description: Execution results
schema:
$ref: '#/definitions/ExecutionResults'
security:
- api_key: []

Required property per path and method

Consider the following sample specification:
swagger: '2.0'
info:
title: stackoverflow question
version: '1.0'
paths:
/websites:
post:
parameters:
- in: body
name: website
required: true
schema:
$ref: '#/definitions/website'
responses:
201:
description: Website created
/websites/{id}:
patch:
parameters:
- name: 'id'
in: 'path'
type: 'integer'
required: true
- in: body
name: fields
required: true
schema:
$ref: '#/definitions/website'
responses:
200:
description: Website updated
definitions:
website:
type: object
required:
- fieldB
properties:
fieldA:
type: string
readOnly: true
fieldB:
type: string
format: date-time
fieldC:
type: string
format: date-time
The fieldB property is required for both POST and PATCH methods. Is it possible to make it required only for a single path and method without having to specify two different model definitions?
If not, what would be a good solution to achieve this?

Swagger - How to write common response field?

I have several APIs, and all of them return JSON with a boolean field named success.
API 1
{"success": true, "data": "some data"}
API 2
{"success": false, "error": "error message"}
Can I write its swagger 2.0 document with something like a template, so I don't need to copy and paste the success field part in each API like this?
responses:
200:
schema:
properties:
success:
type: boolean
description: true if the request is successful.
data:
...
and
responses:
200:
schema:
properties:
success:
type: boolean
description: true if the request is successful.
error:
...
Thanks!
Yes, use allOf for the common fields:
responses:
200:
schema:
allOf:
- $ref: '#/definitions/common'
- properties:
data:
# your details here
definitions:
Common:
type: object
properties:
success:
type: boolean
description: true if the request is successful.
Also:
schema:
allOf:
- $ref: '#/definitions/Common'
- properties:
data:
$ref: '#/definitions/Another'

How to send the Default value through body in Swagger 2.0

Hi i am trying to send the Default values through body parameters but its Not taking while Submitting. can anybody please help me on this issue. Here is my code and i am trying to send the default name parameter through body
swagger: '2.0'
info:
version: 1.0.0
title: PetStore on Heroku
description: |
**This example has a working backend hosted in Heroku**
You can try all HTTP operation described in this Swagger spec.
Find source code of this API [here](https://github.com/mohsen1/petstore-api)
host: petstore-api.herokuapp.com
basePath: /pet
schemes:
- http
- https
consumes:
- application/json
- text/xml
produces:
- application/json
- text/html
paths:
/:
get:
parameters:
- name: limit
in: query
description: number of pets to return
type: integer
default: 0
responses:
200:
description: List all pets
schema:
title: Pets
type: array
items:
$ref: '#/definitions/Pet'
post:
parameters:
- name: pet
in: body
description: The pet JSON you want to post
schema:
$ref: '#/definitions/Pet'
required: true
responses:
200:
description: Make a new pet
put:
parameters:
- name: pet
in: body
description: The pet JSON you want to post
schema:
$ref: '#/definitions/Pet'
required: true
responses:
200:
description: Updates the pet
/{petId}:
get:
parameters:
- name: petId
in: path
type: string
description: ID of the pet
required: true
responses:
200:
description: Sends the pet with pet Id
definitions:
Pet:
type: object
properties:
name:
type: string
default : "xxxxxxx"
birthday:
type: integer
format: int32
The default value should be handled in the server side as the server should not always assume the client sends HTTP requests that are 100% conforming to the spec.
I think this can help you, if you are trying to send default data with your schema:
definitions:
Pet:
type: object
default:
name: xxxx
birthday: xxxx
properties:
name:
type: string
birthday:
type: integer
format: int32

Resources