AWS API Gateway schema validation with custom formats - validation

Can I use my own custom formats in my OpenAPI definition and have the AWS API Gateway validate using them? I can't find any reference for this so I assume not?
For example, I would only like to greet guys named Dave:
swagger: "2.0"
info:
version: "1.0"
title: "Hello World API"
paths:
/hello/{user}:
get:
description: Returns a greeting to the user!
parameters:
- name: user
in: path
type: string
required: true
description: The name of the user to greet.
format: "guys-named-dave"

The documentation on this is a bit implicit, indeed. If you combine this
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-request-validation-set-up.html
with this
https://swagger.io/specification/#schema-object
the following should be a solution, that works (and it does!)
paths:
/hello
get:
x-amazon-apigateway-request-validator: params
parameters:
- name: user
in: path
required: true
schema:
type: string
pattern: ^.*dave.*$
x-amazon-apigateway-request-validators:
params:
validateRequestParameters: true
To allow for case insensitive names like "Dave" and "DAVE", try the pattern /.*dave.*/i. I don't know if this will work.

Related

Unable to Implement Path Parameter Request Validation

I'm attempting to implement an API that does front-line path parameter validation using AWS SAM that will immediately reject calls whose input does not adhere to the rules defined in a SAM Models block.
Despite re-reading the docs and trying countless implementations, I am unable to reject calls that should be rejected. My GetPetFunction still executes when it shouldn't. Any ideas on what I'm doing wrong, looking at the sample from my template below? I do see the model generated on the APIG console with Request Validator set to params-only.
PetApi:
Type: AWS::Serverless::Api
Properties:
Models:
PetModel:
type: object
required:
- pet
- breed
- color
properties:
pet:
type: string
pattern: (dog|cat)
breed:
type: string
pattern: (poodle|tabby|husky)
color:
type: string
GetPetFunction:
Type: AWS::Serverless::Function
Properties:
Events:
ApiCall:
Type: Api
Properties:
Path: /{pet}/{breed}/{color}
Method: get
RestApiId: !Ref PetApi
RequestParameters:
- method.request.path.pet:
Required: true
- method.request.path.breed:
Required: true
- method.request.path.color:
Required: true
RequestModel:
Model: PetModel
Required: true
ValidateParameters: true

How to implement a Spring controller which returns any of multiple response types? (multiple 200 code responses)

I have OpenAPI 3 documentation which defines a method:
/header/somemethod:
get:
tags:
- Layout
summary: some text
responses:
200:
description: success
content:
application/json:
schema:
type: array
items:
anyOf:
- $ref: '#/components/schemas/Item1'
- $ref: '#/components/schemas/Item2'
How I can implement a Spring Boot controller which mathes this documentation?
How I can set up swagger annotations to generate this documentation?
I tried to generate a server in Swagger editor, but generated code doesn't show Item1 and Item2.
Thanks.
I believe you have configured pom.xml file for swagger code generation. To get Item1 and Item2, you need to define them in the above file like below:
paths: # path definitions here
definitions:
Item1:
properties:
Item1_name:
description: name
type: string
Item1_value:
description: value
type: number
Item2:
properties:
Item2_name:
description: name
type: string
Item2_value:
description: value
type: number
Note: Here fields in Item1 and Item2 are having fields name and value

Validate example part of swagger specification against its schema

Is it possible to validate example part of swagger document against corresponding schema?
I've found tools for both validating schema itself (like http://editor.swagger.io or swagger-cli etc - but that's just linting) and validating requests/responses against schema (things like swagger-express-middleware or swagger-assertions).
But I'd like to validate that examples in responses and definitions are consistent with schemas.
For example:
swagger: '2.0'
paths:
/test:
get:
description: test endpoint
produces:
- application/json
responses:
200:
description: OK
schema:
type: object
properties:
name:
type: string
id:
type: integer
example:
name: 13
id: some string
seems to be "correct" for all those tools even if example contains incorrect structure.
is there a tool that can do that?

Gets duplicated mapping key error using openapi 3.0

I am trying to define my APIs using openapi version 3.0.0. I've generated following YAML file:
openapi: 3.0.0
info:
title: My YAML
description: My YAML
version: 1.0.0
servers:
- url: http://my.server.url
paths:
/v1/service/uri:
post:
summary: Getting some information.
parameters:
- name: Content-Type
in: header
description: Content type of request body.
required: true
style: simple
explode: false
schema:
type: string
- name: Host
in: header
description: Originate host which returns the response.
required: false
style: simple
explode: false
schema:
type: string
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MyPostRequest'
example:
my_name: "zizi"
my_age: 29
required: true
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/MyPostResponse'
components:
schemas:
MyPostRequest:
type: object
properties:
my_name:
type: string
my_age:
type: integer
format: int32
MyPostResponse:
type: object
properties:
status:
type: integer
format: int32
When I copy/paste these lines into Swagger Editor, it gives me duplicated mapping key error on line 19; it is for section description of parameter Content-Type.
I have studied openapi documentation, but I didn't see anything wrong with my YAML file.
I am not sure why you get that error, I tried to find out in which language Swagger is written, as there are several YAML parsers out there that are known to have problems, but couldn't easily determine that using google or wikipedia.
You don't have any duplicate keys in your file, but it is invalid (i.e. not valid YAML) because of the indentation of the key content (the second occurrence, the one under paths →
/v1/service/uri → post → responses → 200), that should be aligned with
description as that key cannot have a value node that is both a scalar (OK) as well as a mapping node content: ....

how to generate swagger document with tags using serverless-aws-documentation plugin for serverless

I am using serverless-aws-documentation plugin to auto-generate swagger-doc. Followed all the steps provided at : https://github.com/9cookies/serverless-aws-documentation. Under documentation key I am defining tags but it is not getting generated in the output swagger doc. Following is the sample handler :
functions:
get_tickets:
handler: handler.ticket_handler.get_tickets
events:
- http:
path: tickets
method: get
cors: true
documentation:
tags:
- private
- admin
summary: "Get list of ticket"
description: "This ticket will provide you list of tickets"
I want to segrigate APIs depending on the tags, but not able to achieve it. Thanks in advance for the help.
Try to add the serverless-aws-documentation plugin in the serverless.yml
plugins:
- serverless-aws-documentation
Add the infor and models documentation in the custom section:
custom:
myStage: ${opt:stage, self:provider.stage}
profiles:
dev: road-we-go
prod: road-we-
documentation:
info:
version: "1.0.0"
name: "Example API"
description: "Example API description"
termsOfService: "https://example.com/terms-of-service"
contact:
name: "Example API"
url: "https://example.com"
email: "dev#example.com"
licence:
name: "Licensing"
url: "https://example.com/licensing"
models:
-
name: "StoreAudioSuccess"
description: "Model for store audio"
contentType: "application/json"
schema: ${file(swagger/audios/storeResponse.
Add the function documentation:
If you want to add the custom models like RequestStore and StoreAudioSuccess check the serverless-aws-documentation documentation and the json-schema docs
functions:
Update:
handler: src/functions/update.handler
timeout: 30
memory: 128
events:
- http:
method: put
private: true
cors: true
path: api/v1/update
documentation:
summary: "Update "
description: "Update a record"
tags:
- "Users"
requestModels:
"application/json": "RequestStore"
methodResponses:
-
statusCode: "200"
responseModels:
"application/json": "StoreUserSuccess"
To download the swagger documentation you need to run this command:
First you need to deploy you project
sls downloadDocumentation --outputFileName=swagger.json
Which version are you using?
According to their latest documentation https://github.com/9cookies/serverless-aws-documentation, you need to provide tags as follows i.e. within double quotes.
documentation:
tags:
- "private"
- "admin"

Resources