Describing properties in swagger example - json

Try to add multiline example in definition of JSON response in swagger. Need to get structure like
{
"result": {
"urls": {
"site/index": true,
"site/create": true,
"site/update": true,
"site/delete": true,
"site/types": true,
"site/save": false
}
}
}
but now everything I've got only
{
"result": {
"urls": [
{
"site/index": true
},
{
"site/create": true
},
{
"site/update": true
},
{
"site/delete": true
},
{
"site/types": true
},
{
"site/save": false
}
]
}
}
My YAML file looks like
swagger: "2.0"
info:
description: "xxx"
version: 1.2.8
title: "xxx"
host: "xxx"
basePath: "xxx"
schemes:
- "https"
consumes:
- application/json
produces:
- application/json
/site/check:
post:
summary: ""
description: ""
parameters:
- in: body
name: urls
required: true
schema:
$ref: '#/definitions/CheckAccess'
responses:
200:
description: OK
schema:
$ref: '#/definitions/CheckAccessOKResponse'
definitions:
CheckAccess:
properties:
urls:
type: string
example:
- "site/index"
- "site/create"
- "site/update"
- "site/delete"
- "site/types"
- "site/save"
required:
- urls
CheckAccessOKResponse:
properties:
result:
type: object
properties:
urls:
type: object
example:
- "site/index": true
- "site/create": true
- "site/update": true
- "site/delete": true
- "site/types": true
- "site/save": false
Strings in example can be different, it's just a block of paths.
How can I fix my example or structure of CheckAccessOKResponse to get the needed view of JSON?
Thank you.

Remove the leading dashes from the example value list. Dashes indicate array items, you don't need that.
Also, result.urls is currently defined as a free-form object, but you probably want a string-to-boolean map instead.
CheckAccessOKResponse:
properties:
result:
type: object
properties:
urls:
type: object
additionalProperties: # <--- this means the object is
type: boolean # a string-to-boolean map
example:
"site/index": true # <--- no leading dashes
"site/create": true
"site/update": true
"site/delete": true
"site/types": true
"site/save": false

Related

how do I validate a JSON payload in OpenAPI 3.0 with Mule 4.4 Runtime

I need to develop a Mule API ( 4.4 Runtime ) with openapi: 3.0.0
The endpoint is a POST with the following request payload :
{
"Employee": {
"Address": {
"City": "a",
"Country": "aaa"
}
}
}
Here is the relevant section of the OpenAPI 3.0 spec :
openapi: "3.0.0"
paths:
/search:
post:
tags:
- SearchUser
summary: Search for Users
operationId: getUser
requestBody:
description: User Request Object
content:
application/json:
schema:
$ref: '#/components/schemas/RequestComp'
required: true
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ResponseComp'
'400':
description: Bad request
content: {}
'404':
description: User not found
content: {}
'405':
description: Validation exception
content: {}
components:
schemas:
RequestComp:
type: object
properties:
Employee:
$ref: '#/components/schemas/EmployeeComp'
EmployeeComp:
type: object
properties:
Address:
$ref: '#/components/schemas/AddressComp'
AddressComp:
type: object
properties:
City:
type: string
required: true
nullable: false
minLength: 1
Country:
type: string
required: true
nullable: false
minLength: 1
ResponseComp:
type: object
properties:
City:
type: string
Country:
type: string
So I can validate individual elements like 'City' and 'Country' to not be null but how do I prevent following request ? ( presently its not being flagged as invalid : )
{
"Address": {
"City": "a",
"Country": "aaa"
}
}
You can define the Employee wrapper as a required property and also disallow unknown properties by adding additionalProperties: false. Note that required is not a property-level attribute, but an object-level attribute - it's a list of required properties.
components:
schemas:
RequestComp:
type: object
required: [Employee] # <-----
properties:
Employee:
$ref: '#/components/schemas/EmployeeComp'
additionalProperties: false # <-----
EmployeeComp:
type: object
properties:
Address:
$ref: '#/components/schemas/AddressComp'
additionalProperties: false # <-----
AddressComp:
type: object
required: [City, Country] # <-----
properties:
City:
type: string
# required: true # <-- remove this
nullable: false
minLength: 1
Country:
type: string
# required: true # <-- remove this
nullable: false
minLength: 1
additionalProperties: false # <-----

Avoid additional fields in json apart from the fields defined in the swagger to fail the validation in WSO2 APIM 3.1.0

We are trying to validate a json in APIM 3.1.0 by defining the validation conditions in swagger definition and by enabling the schema validation under runtime configurations. PFA the Swagger definition.
When additional fields are passed in the request json apart from the fields defined in the swagger, the validation must fail or gateway should ignore those additional fields and shouldn't be passing it to the backend. But this is not happening currently, Could you please suggest us if we are missing anything here.
Sample Request JSON
Note : In this JSON, "test" field is an additional parameter sent in the request.
{​​​​​​​
"applicationId": "Test123_3211",
"name": "Bala krishna",
"dateOfBirth": "1981-04-11",
"gender": "FEMALE",
"phonenumber": "9039283630",
"altphonenumber": "9979979971",
"panCard": "AAAAV1234N",
"nomineeName": "Ramji Ambedkar",
"nomineeDOB": "1976-04-14",
"source": "ONLINE",
"process": "Canara HSBC",
"callDate": "2018-08-24",
"callTime": "17:00",
"merType": "VIDEOMER",
"instantCall": true,
"test": "abcd"
}​​​​​​​
Below is the swagger API :
swagger: "2.0"
info:
version: v1.0.0
title: MedicalBookAppointmentAPI
description: "This API is for booking medical appointments. \n\nSupported operations :\n\n\t1. Docs APP "
schemes:
https
http
produces:
application/json
paths:
/docs-app:
post:
summary: This API will be used to add cases and to schedule appointments.
description: This API will be used to add cases and to schedule appointments.
parameters:
- in: body
name: Payload
description: Request Body
required: true
schema:
$ref: "#/definitions/docs-app-request"
responses:
"200":
description: OK
schema:
$ref: "#/definitions/docs-app-response"
"400":
schema:
$ref: "#/definitions/book-appointment-api-error"
description: Bad Request. Invalid request or validation error.
"415":
schema:
$ref: "#/definitions/book-appointment-api-error"
description: " Unsupported Media Type. The entity of the request was in a not supported format."
"500":
schema:
$ref: "#/definitions/book-appointment-api-error"
description: Internal Server Error
produces:
- application/json
consumes:
- application/json
x-auth-type: "Application & Application User"
x-throttling-tier: Unlimited
definitions:
docs-app-request:
type: "object"
required:
- "applicationId"
- "process"
properties:
applicationId:
type: "string"
name:
type: "string"
dateOfBirth:
type: "string"
pattern: "^\d{​​​​​​​4}​​​​​​​-\d{​​​​​​​2}​​​​​​​-\d{​​​​​​​2}​​​​​​​$"
gender:
type: "string"
phonenumber:
type: "number"
altphonenumber:
type: "number"
panCard:
type: "string"
nomineeName:
type: "string"
nomineeDOB:
type: "string"
pattern: "^\d{​​​​​​​4}​​​​​​​-\d{​​​​​​​2}​​​​​​​-\d{​​​​​​​2}​​​​​​​$"
source:
type: "string"
process:
type: "string"
planDetails:
type: "string"
priorityStatus:
type: "string"
callDate:
type: "string"
pattern: "^\d{​​​​​​​4}​​​​​​​-\d{​​​​​​​2}​​​​​​​-\d{​​​​​​​2}​​​​​​​$"
callTime:
type: "string"
metaInfo:
type: "string"
instantCall:
type: "boolean"
merType:
type: "string"
enum:
- "TELEMER"
- "VIDEOMER"
- "OTHERS"
docs-app-response:
type: object
properties:
data:
type: object
properties:
approved:
type: boolean
isInternational:
type: boolean
id:
format: int64
type: integer
applicationId:
type: string
name:
type: string
dateOfBirth:
type: string
gender:
type: string
phonenumber:
type: string
altphonenumber:
type: string
panCard:
type: string
nomineeName:
type: string
nomineeDOB:
type: string
merType:
type: string
vendor:
type: string
updatedAt:
type: string
createdAt:
type: string
success:
type: integer
book-appointment-api-error:
title: Error object returned with HTTP status
type: object
properties:
fault:
type: object
properties:
code:
format: int64
type: integer
type:
type: string
message:
description: Error message.
type: string
description:
description: A detail description about the error message.
type: string
required:
- code
- message
You can use either additionalProperties or minProperties/maxProperties in the swagger file and restrict the additional properties in the payload.
If you know the property count to be limited then use minProperties/maxProperties
If you don't know the property count to be limited then use additionalProperties
For more details please refer https://m-saranki.medium.com/unboxing-json-schema-validator-320-2dd944dae6c0

Is there a way to convert OpenAPI specifications to json mappings in maven?

I need to convert OpenAPI specifications into json mappings so I could use them in my wiremock server, but I'm not sure if there is an available plugin to do this type of conversion.
This is an example of an OpenAPI I'm using
openapi: "3.0.0"
paths:
/fraudcheck:
put:
summary: Perform Fraud Check
x-contracts:
- contractId: 1
name: Should Mark Client as Fraud
priority: 1
requestBody:
content:
application/json:
schema:
type: object
properties:
"client.id":
type: integer
loanAmount:
type: integer
x-contracts:
- contractId: 1
headers:
Content-Type: application/json
body:
"client.id": 1234567890
loanAmount: 99999
matchers:
body:
- path: $.['client.id']
type: by_regex
value: "[0-9]{10}"
responses:
'200':
description: created ok
content:
application/json:
schema:
type: object
properties:
fraudCheckStatus:
type: string
"rejection.reason":
type: string
And this is the json output I would like to have
{
"id" : "d5966bb3-554e-4b83-b18b-77ca22e2a439",
"request" : {
"url" : "/fraudcheck",
"method" : "PUT",
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
}
},
"response" : {
"status" : 200,
"body" : "{\"fraudCheckStatus\":\"FRAUD\",\"rejection.reason\":\"Amount too high\"}",
"headers" : {
"Content-Type" : "application/json;charset=UTF-8"
}
}

OpenApiSpecification JSON inside of another JSON

Hey guys I'm trying to create OAS3 200 response which returns JSON inside of JSON. The problem is I simply don't know what syntax I should use here. I tried to make a double reference but I don't know if it's possible to do that way.
{
"success": false,
"messageType": "error",
"error": {
"apiKey": "apiKey must not be empty"
}
}
responses:
'200':
description: Upon successful generated invoice you will be returned with the following JSON.
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/success'
- $ref: '#/components/schemas/invalid'
components:
schemas:
invalid:
type: object
properties:
success:
description: Says if the invoice was generated successfully.
type: boolean
example: false
messageType:
description: Type of the message
type: string
example: error
error:
schema:
$ref: '#/def/data'
def:
data:
type: object
properties:
apikey:
description: lol
type: string
example: lolol
You are almost there. Define your data schema in components/schemas, and make the error property a $ref to this schema.
components:
schemas:
invalid:
type: object
properties:
success:
...
messageType:
...
error:
$ref: '#/components/schemas/data' # <--------
data: # <--------
type: object
properties:
apikey:
description: lol
type: string
example: lolol
Another way is to define the nested schema inline:
components:
schemas:
invalid:
type: object
properties:
success:
...
messageType:
...
error:
type: object # <--------
properties:
apikey:
description: lol
type: string
example: lolol

How to create an API definition in Swagger that contains different objects in different levels?

I've been trying to develop an API in Swagger using YAML. I want to have a global document definition that reads as it follows in JSON (example):
{
"document_info" :
{
"name" : "string",
"file" : "base64"
},
"document_details":
{
"author" : "string",
"keywords" : "string"
},
"page_parameters" :
{
"start_page" : "integer",
"end_page" : "integer"
},
"extraction_operations":
{
"extract_toc" : "bool",
"extract_page" : "bool"
}
}
Right now I'm using allOf YAML function to get the contents from document_info, document_details, page_parameters and extraction_operations and it is producing me the following result (not quite the expected):
{
"file": "string",
"name": "string",
"author": "string",
"subject": "string",
"title": "string",
"creator": "string",
"keywords": [
"string"
],
"startPage": 0,
"endPage": 0,
"extractCover": true,
"extractDetails": true,
"extractTOC": true,
"extractPages": true,
"extractClipped": true,
"extractFiles": true,
"extractImages": true,
"extractLinks": true,
"extractMerge": true
}
It is functional like this but what I wanted was to have something way easier to read and to work with just like what was mentioned above. My current YAML definition for this is represented bellow:
definitions:
APIFileExtract:
type: object
required:
- file
- name
properties:
file:
type: string
name:
type: string
APIFileExtractWithPageParams:
allOf:
- $ref: '#/definitions/APIFileExtract'
- $ref: '#/definitions/APIFileExtractPageParams'
APIFileExtractDetails:
allOf:
- $ref: '#/definitions/APIFileExtract'
- type: object
properties:
author:
type: string
subject:
type: string
title:
type: string
creator:
type: string
keywords:
type: array
items:
type: string
APIFileExtractMethods:
type: object
properties:
extractCover:
type: boolean
extractDetails:
type: boolean
extractTOC:
type: boolean
extractPages:
type: boolean
extractClipped:
type: boolean
extractFiles:
type: boolean
extractImages:
type: boolean
extractLinks:
type: boolean
extractMerge:
type: boolean
APIFileExtractPageParams:
type: object
properties:
startPage:
type: integer
endPage:
type: integer
APIFileExtractGlobal:
allOf:
- $ref: '#/definitions/APIFileExtract'
- $ref: '#/definitions/APIFileExtractDetails'
- $ref: '#/definitions/APIFileExtractPageParams'
- $ref: '#/definitions/APIFileExtractMethods'
Is it possible to format the way I pretend to? Can someone give me some guidelines on how to do it in Swagger?
Made it!
Had to nest things like this:
APIFileExtractGlobal:
type: object
properties:
DocumentInfo:
$ref: '#/definitions/APIFileExtract'
DocumentDetails:
$ref: '#/definitions/APIFileExtractDetails'
PageParameters:
$ref: '#/definitions/APIFileExtractPageParams'
ExtractionMethods:
$ref: '#/definitions/APIFileExtractMethods'