VehicleBaseAttributes:
type: object
properties:
id:
type: integer
format: int64
model:
type: string
doors:
type: integer
VehicleExtendedAttributes:
type: object
properties:
$ref: '#/components/schemas/VehicleBaseAttributes'
pricehistory:
type: array
items:
title: PriceHistory
type: object
properties:
priceWOT:
type: number
taxes:
type: number
additionalCosts:
type: number
price:
type: number
createdByUserId:
type: string
createdDate:
type: string
dealerHistory:
type: array
items:
title: DealerHistory
type: object
properties:
name:
type: string
phone:
type: string
createdByUserId:
type: string
createdDate:
type: string
In the above example I intended to have a basic set of attributes defined, then offer an extended version that used the base version.
Obviously in the VehicleExtendedAttributes I don't want to nest the VehicleBaseAttributes in a separate attribute, instead for them to be added to the top level, resulting in an output of something like:
type: object
properties:
id:
type: integer
format: int64
model:
type: string
doors:
type: integer
pricehistory:
type: array
items:
title: PriceHistory
type: object
properties:
priceWOT:
type: number
taxes:
type: number
additionalCosts:
type: number
price:
type: number
createdByUserId:
type: string
createdDate:
type: string
dealerHistory:
type: array
items:
title: DealerHistory
type: object
properties:
name:
type: string
phone:
type: string
createdByUserId:
type: string
createdDate:
type: string
The problem is this results in an error:
Schema error at components.schemas['VehicleExtendedAttributes']
should have required property '$ref'
missingProperty: $ref
Jump to line 342
Schema error at components.schemas['VehicleExtendedAttributes']
should match exactly one schema in oneOf
Jump to line 342
Schema error at components.schemas['VehicleExtendedAttributes'].properties['$ref']
should be object
Jump to line 345
You need allOf to do model composition. The value of allOf is a list of subschemas ($referenced or inline) that together compose a single schema.
VehicleExtendedAttributes:
allOf:
- $ref: '#/components/schemas/VehicleBaseAttributes'
- type: object
properties:
pricehistory:
type: array
...
dealerHistory:
type: array
...
Further info:
Swagger Inheritance and Composition here on Stack Overflow
Composition and Inheritance (Polymorphism) section of the OpenAPI 3.0 Specification
Model Composition, Inheritance and Polymorphism
oneOf, anyOf, allOf, not
Related
I have an input in the below format:
{"value" : {"codes": ["123","234"]} }
I have a swagger document as below but it does not yield the above result
/v1/search:
post:
summary: Get values
description: Get values
parameters:
- name: value
in: body
schema:
properties:
value:
type: array
items:
$ref: "#/definitions/Values"
definitions:
Values:
type: object
properties:
codes:
type: string
Any leads would be appreciated.
Try this. I used Value and Codes as schema names, but feel free to change them as you see fit.
- name: value
in: body
schema:
$ref: '#/definitions/Value'
definitions:
Value:
type: object
properties:
value:
$ref: '#/definitions/Codes'
Codes:
type: object
properties:
codes:
type: array
items:
type: string
I have the following API definition in SwaggerHub:
swagger: '2.0'
info:
description: defaultDescription
version: '0.1'
title: defaultTitle
host: swapi.co
paths:
/api/people:
get:
produces:
- application/json
parameters:
- name: search
in: query
required: false
type: string
x-example: luke
responses:
'200':
description: Definition generated from Swagger Inspector
schema:
$ref: '#/definitions/Model0'
responseSchema:
$ref: '#/definitions/Model0'
definitions:
Results:
properties:
name:
type: string
height:
type: string
mass:
type: string
hair_color:
type: string
skin_color:
type: string
eye_color:
type: string
birth_year:
type: string
gender:
type: string
homeworld:
type: string
films:
type: array
items:
type: string
species:
type: array
items:
type: string
vehicles:
type: array
items:
type: string
starships:
type: array
items:
type: string
created:
type: string
edited:
type: string
url:
type: string
Model0:
properties:
count:
type: integer
format: int32
next:
type: object
previous:
type: object
results:
type: array
items:
$ref: '#/definitions/Results'
I cannot make this basic GET command to bring back the data I'm seeking. It only returns this:
TypeError: Failed to fetch
I'm unsure if it's a syntax issue, or possibly spacing, but I'm also getting an error for line 19 that reads:
should NOT have additional properties
additionalProperty: responseSchema, description, schema
Any ideas what is wrong?
https://swapi.co seems to be HTTPS-only, so you need to add
schemes:
- https
to you API definition to specify the protocol for requests.
but I'm also getting an error for line 19 that reads: "should NOT have additional properties additionalProperty: responseSchema, description, schema".
Remove these lines:
responseSchema:
$ref: '#/definitions/Model0'
There's no responseSchema keyword in OpenAPI.
I'm getting the following error:
Schema error at paths./cards/count.get.parameters[0] is not exactly
one from <#/definitions/parameter>,<#/definitions/jsonReference>
Here is my definition:
/cards/count:
get:
tags:
- "cards"
summary: "Number of Cards available"
description: "Excludes cards which the user has answered correctly in the past."
operationId: "countCards"
produces:
- "application/json"
parameters:
- name: "tagFilter"
in: "query"
description: "Input is optional - left blank will return all tags and a count"
type: "object"
properties:
tags_any:
type: "array"
items:
type: "integer"
format: "int64"
enum: [1,2,3]
tags_all:
type: "array"
items:
type: "integer"
format: "int64"
enum: [4,5,6]
tags_not:
type: "array"
items:
type: "integer"
format: "int64"
enum: [4,5,6]
I understand you can't use a schema definition as per this question: Swagger: Reusing an enum definition as query parameter
What do I need to modify to make the YAML compile without errors?
Objects in query parameters are not supported in OpenAPI/Swagger 2.0, but are supported in OpenAPI 3.0.
If you stick with OpenAPI 2.0, you'll need to split the object into separate parameters, like so:
parameters:
- in: query
name: tags_any
type: array
items:
type: integer
format: int64
enum: [1,2,3]
- in: query
name: tags_all
type: array
items:
type: integer
format: int64
enum: [4,5,6]
- in: query
name: tags_not
type: array
items:
type: integer
format: int64
enum: [4,5,6]
In OpenAPI 3.0, you can define the parameter as object and use the style and explode keywords to specify how this object should be serialized.
parameters:
- name: tagFilter
in: query
description: Input is optional - left blank will return all tags and a count
# Send the object as ?prop1=value1&prop2=value2
# This is the default serialization method for objects in query parameters
style: form
explode: true
schema:
type: object
properties:
tags_any:
type: array
items:
type: integer
format: int64
enum: [1,2,3]
tags_all:
type: array
items:
type: integer
format: int64
enum: [4,5,6]
tags_not:
type: array
items:
type: integer
format: int64
enum: [4,5,6]
Code below:
definitions:
Result:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
FindUID:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
data:
type: object
properties:
uid:
type: integer
format: int64
FindUsername:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
data:
type: object
properties:
username:
type: string
As you can see, the first part of FindUID and FindUsername is the same as Result. How to replace those duplicate code with Result?
You can compose definitions using allOf, here's a full example where Result is used in FindUID and FindUsername:
swagger: '2.0'
info:
description: Example API Description
title: Example Title
version: 1.0.0
paths: {}
definitions:
Result:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
FindUID:
allOf:
- $ref: "#/definitions/Result"
- type: object
properties:
data:
type: object
properties:
uid:
type: integer
format: int64
FindUsername:
allOf:
- $ref: "#/definitions/Result"
- type: object
properties:
data:
type: object
properties:
username:
type: string
More about this here: https://apihandyman.io/writing-openapi-swagger-specification-tutorial-part-4-advanced-data-modeling/ (disclosure: I wrote this tutorial)
How to write documentation in swagger for array of objects. This is my code, but I don't know how to access data in array of objects.
{
"first_name":"Sam",
"last_name":"Smith",
"reservations":[
{
"chkin_date":"2016-03-31",
"chkout_date":"2016-04-08",
"adults":1,
"children":2,
"chdage":[2,3]
},
{
"chkin_date":"2016-03-30",
"chkout_date":"2016-04-03",
"adults":1,
"children":2,
"chdage":[2,3,5]
}
]
}
Here you go:
definitions:
SomeObject:
properties:
first_name:
type: string
example: Sam
last_name:
type: string
example: Smith
reservations:
type: array
items:
$ref: '#/definitions/Reservation'
Reservation:
properties:
chkin_date:
type: string
format: date
example: 2016-03-31
chkout_date:
type: string
format: date
example: 2016-04-08
adults:
type: integer
format: int32
example: 1
children:
type: integer
format: int32
example: 2
chdage:
type: array
items:
type: integer
format: int32