swagger limit error codes of response - rest

There is a swagger 2.0 file I am dealing with, where I want to specify that the error code from my response can only be USERNAME_VALIDATION_FAILED or EMAIL_VALIDATION_FAILED. This is due to a swagger-ui view of the swaggerfile where every response error can have the 20 error codes defined in the Error code enum.
How to limit the possible error codes for a specific response?
In my swaggerfile there is the request
/register:
post:
...
parameters:
...
responses:
...
400:
description: Username or email validation failed, possible values for code are USERNAME_VALIDATION_FAILED, EMAIL_VALIDATION_FAILED
schema:
$ref: '#/definitions/Error'
and my Error looks like
Error:
type: object
properties:
code:
type: string
enum:
- USERNAME_VALIDATION_FAILED
- EMAIL_VALIDATION_FAILED
- USERNAME_EXISTS
- ...
I would suggest something like
schema:
$ref: '#/definitions/Error.code.of(USERNAME_VALIDATION_FAILED, EMAIL_VALIDATION_FAILED)'

You can't override the enum of a property, so you need a separate model:
RegistrationError:
type: object
properties:
code:
type: string
enum:
- USERNAME_VALIDATION_FAILED
- EMAIL_VALIDATION_FAILED
Error:
type: object
properties:
code:
type: string
enum:
- USERNAME_VALIDATION_FAILED
- EMAIL_VALIDATION_FAILED
- USERNAME_EXISTS
- ...
If the error models have common properties, you can "inherit" them from a base model to reduce code duplication:
BaseError:
type: object
properties:
message:
type: string
RegistrationError:
allOf:
- $ref: "#/definitions/BaseError"
- type: object
properties:
code:
type: string
enum:
- USERNAME_VALIDATION_FAILED
- EMAIL_VALIDATION_FAILED
Error:
allOf:
- $ref: "#/definitions/BaseError"
- type: object
properties:
code:
type: string
enum:
- USERNAME_VALIDATION_FAILED
- EMAIL_VALIDATION_FAILED
- USERNAME_EXISTS
- ...

Related

Creating two ALB with exception if value put in instance id then will create alb, but Not able to pass two instance id values in parameter section

Please help me with the I'm using to create two ALB with the exception if a value is put in one of the parameters then will create an alb that has the instances id but if I pass two instances id in the parameter ELB1InstanceIds then it gave me an error:
Properties validation failed for resource HTTPTG1 with message: #/Targets/0/Id: expected type: String, found: JSONArray
earlier I was using parameter `Type: 'ListAWS::EC2::Instance::Id' but when i don't select any instance in ELB2InstanceIds then I got an error that it required a selected value then only stack can be run.
so I have created with the type string and passed an instance id, it is working with one instance id but not working when I specify two instances id in one of the parameters.
Parameters:
ELB1InstanceIds:
Type: String
Default: i-12345678
ELB2InstanceIds:
Type: String
Default: i-12345678
Conditions:
IsELB1InstanceIdsNotEmpty: !Not [!Equals [!Ref ELB1InstanceIds, "i-12345678"]]
IsELB2InstanceIdsNotEmpty: !Not [!Equals [!Ref ELB2InstanceIds, "i-12345678"]]
Resources:
HTTPTG1:
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties:
Targets:
- Id: !If
- IsELB1InstanceIdsNotEmpty
- - !Split [",", !Ref ELB1InstanceIds]
- !Ref 'AWS::NoValue'
HTTPTG2:
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties:
Targets:
- Id: !If
- IsELB2InstanceIdsNotEmpty
- - !Split [",", !Ref ELB2InstanceIds]
- !Ref 'AWS::NoValue'
earlier I was using two parameters ELB1InstanceIds, and ELB2InstanceIds with Type: 'List<AWS::EC2::Instance::Id>' but when I don't select any instance in ELB2InstanceIds then I got an error that it required a selected value then only stack can be run.

Cannot pass dynamic query parameters when using `express-openapi-validator`

The idea is taken from here stack-overflow
After adding a parameter that is supposed do allow dynamic query parameters, it gives error.
Query Example:
/pets:
get:
description: |
Returns all pets
operationId: findPets
parameters:
- name: params
in: query
required: false
schema:
type: object
# If the parameter values are of specific type, e.g. string:
# additionalProperties:
# type: string
# If the parameter values can be of different types
# (e.g. string, number, boolean, ...)
additionalProperties: true
# `style: form` and `explode: true` is the default serialization method
# for query parameters, so these keywords can be omitted
style: form
explode: true
responses:
'200':
description: pet response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
Executing a query, returns
{"message":"Cannot convert undefined or null to object"}
To Reproduce
Clone this repository
run npm install
run npm start
run curl http://localhost:3000/v1/pets\?type\=dog\&limit\=10\&test\=query
Expected behavior
It must allow all the query strings
This was the bug in the express-openapi-validator package.
It is now fixed in v4.4.2
To test out the functionality, see this example project

How to implement an empty path in YAML file

I am trying to implement the below calls:
POST https://host/sessions
DELETE https://host/sessions/{session_id}
The POST call is to establish a session, the DELETE call is to log out an established session.
So, in the YAML file, how to have an empty base path? It's currently a slash in the YAML file as it's a required filed, but the slash is redundant. Any idea? Thanks.
swagger: '2.0'
info:
version: '0.0.1'
title: authenticate
#description: To be provided
# #termsOfService:To be provided
contact:
name: test
basePath: /sessions
paths:
/:
post:
summary: eatablish a session
description: sessions is a collection.This POST creates a new session in the sessions collection and the name of the session returned by this command is the session token.
consumes:
- "application/json"
parameters:
- in: header
name: user_name
type: string
required: true
- in: header
name: password
type: string
required: true
responses:
200:
description: establish a session successfully
400:
$ref: "#/responses/BadRequest"
500:
description: unexpected error
schema:
$ref: '#/definitions/errorModel'
/{session_id}:
delete:
summary: log out
description: use sessionid to log out an established session.
produces:
- application/json
parameters:
- in: path
name: session_id
type: string
required: true
responses:
200:
description: log out a session successfully
400:
$ref: "#/responses/BadRequest"
500:
description: unexpected error
schema:
$ref: '#/definitions/errorModel'
Swagger defines
A relative path to an individual endpoint. The field name MUST begin with a forward slash (/).
Therefore, the slash is required and you can't have an empty path.

Is it possible to override the "required" attribute of a referenced parameter in OpenAPI 3?

I'm building a simple OpenAPI 3 YAML specification like this:
paths:
/query:
get:
parameters:
- $ref: '#/components/parameters/bookid'
components:
parameters:
bookid:
in: query
name: bookid
required: false
schema:
format: integer
type: number
Now, I'd like to use the common bookid parameter but overriding the required value from false to true. For example (that doesn't work!!!):
paths:
...
/query2:
get:
parameters:
- $ref: '#/components/parameters/bookid'
required: true # <--- ???
Is there a way to do that?
This is not supported. You need separate parameter definitions for required and optional parameters.
As of OpenAPI 3.1, you can only override the description of a referenced parameter, but not other attributes (required, name and others).
# openapi: 3.1.0
parameters:
# This works
- $ref: '#/components/parameters/bookid'
description: Custom description
# This won't work - cannot override attributes other than "description"
- $ref: '#/components/parameters/bookid'
required: true
Here are existing feature requests:
Extend/override properties of a parameter
Allow required as sibling of $ref (like summary/description)

Is there a way to override properties' description and example in OAS3?

I have been looking for resources about inheritance in OAS3 but the closest i get is https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md and the above did not have answer i am looking for.
This is the working example
components:
schemas:
Pet:
properties:
no_legs:
description: "Number of legs"
type: number
example: 4
Duck:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
no_legs:
example: 2
properties:
no_legs:
description: 'Number of webbed feet'
Failing example that was inspired by the spec
Duck:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
no_legs:
description: 'Number of webbed feet'
example: 2
My questions are
Is the overriding feature i am looking at available/supported?
If so what is the appropriate way of doing it?
I understood that i can use composition to tackle this issue but i will have a lot of the definition being repeated.
Yes, that's essentially the way to go about overriding properties. What sort of error are you getting on your failing example? It works just fine at https://editor.swagger.io/ at least. Did you remember to specify openapi: 3.0.0 at the root of your document?
Note that the following two definitions are identical:
Duck:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
no_legs:
description: 'Number of webbed feet'
example: 2
Duck:
allOf:
- $ref: '#/components/schemas/Pet'
properties:
no_legs:
description: 'Number of webbed feet'
example: 2