How can I fix should NOT have additional properties additionalProperty: oneOf? - yaml

I making swagger specs and I have an array of objects on my response. How can I remove this error "should NOT have additional properties additionalProperty: oneOf"
acctBals:
type: array
items:
oneOf:
- $ref: '#/definitions/dog'
- $ref: '#/definitions/cat'
- $ref: '#/definitions/lion'
- $ref: '#/definitions/wolf'
- $ref: '#/definitions/tiger'
This is the example object inside the array.
dog:
type: object
properties:
balType:
example: "Example"
type: "string"
curAmt:
type: object
properties:
amt:
example: 11000
type: "number"
curCode:
example: "PHP"
type: "string"
curConvertRule:
example: null
type: "string"
curRate:
example: null
type: "string"

Related

Enumerating an array of exactly 2 (slightly different) objects in OpenAPI 3.1.0?

I need to document a REST endpoint that takes the following request body:
{
"variables": [
{
"name": "groupGuid",
"value": "...",
"typeConstraint": "string",
},
{
"name": "addMembership",
"value": "...",
"typeConstraint": "boolean",
}
]
}
The variables array must take exactly 2 objects:
one with the required name groupGuid and typeConstraint string,
and the other with the required name addMembership and typeConstraint boolean.
The type of their respective values are specified by typeConstraint, but the actual values of the value properties are otherwise unconstrained.
Currently, I've got this, which is very underspecified (and possibly wrong) and relies on notes I've manually included:
'/test':
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
variables:
type: array
uniqueItems: true
description: 'Must contain exactly 2 objects, with the following `name`s: (1) `groupGuid`: GUID of the group, (2) `addMembership`: Whether the task will add (true) or remove (false) members from the group.'
items:
type: object
additionalProperties: false
properties:
name:
enum:
- groupGuid
- addMembership
type: string
value:
type:
- string
- boolean
typeConstraint:
type: string
enum:
- string
- boolean
description: The type of `value`.
Is it possible to properly spec these requirements in YAML / OpenAPI 3.1.0, and if so, how? Thanks.
If what you want is to show both objects in the example body and accept either or both of them, perhaps you could use anyOf.
Below an example
paths:
/test:
post:
tags:
- Test
description: "Must contain exactly 2 objects, with the following `name`s: (1) `groupGuid`: GUID of the group, (2) `addMembership`: Whether the task will add (true) or remove (false) members from the group."
requestBody:
required: true
content:
application/json:
schema:
properties:
variables:
type: array
uniqueItems: true
items:
anyOf:
- $ref: '#/components/schemas/Object1'
- $ref: '#/components/schemas/Object2'
responses:
'200':
description: Sucess
components:
schemas:
Object1:
type: object
description: "The value here is string"
properties:
name:
type: string
value:
type: string
typeConstraint:
type: string
Object2:
type: object
description: "The value here is boolean"
properties:
name:
type: string
value:
type: string
typeConstraint:
type: boolean

How to define Object with multiple properties including an array of objects in OpenAPI?

I am struggling to define a request body in OpenApi. I want to force the user to send me data in the following shape:
{
"test": {
"a": "a",
"b": "b",
"abc": [
{
"type": "abc",
"name": "name"
},
{
"type": "abc",
"name": "name"
}
...more
]
}
}
This is what I tried but it does not seem to work:
schemas:
SampleRequest:
required:
- abc
properties:
abc:
$ref: "#/components/schemas/ABCType"
ABCType:
type: object
required:
- test
properties:
test:
type: array
items:
type: object
properties:
type:
type: string
name:
type: string
Any help appreciated. Thank you
With the currect json I couldn't make such sense but created this. This created a json which replicates yours.
ABCType:
type: object
additionalProperties: false
required:
- a
- b
- abc
properties:
a:
description: A of ABCType
type: string
b:
description: B of ABCType
type: string
abc:
description: Array of ABCType
type: array
items:
$ref: '#/components/schemas/ABCArrayValue'
ABCArrayValue:
type: object
additionalProperties: false
required:
- type
- name
properties:
type:
description: Type of this array value
type: string
name:
description: Name of this array value
type: string

OpenAPI: overriding a reference, referencing a single field

I'm using two files :
common.yaml contains :
components:
schemas:
GenericError:
type: object
properties:
code:
description: The error code.
type: integer
message:
description: The error description.
type: string
details:
description: Optional details.
type: object
examples:
Bamboozled:
value:
code: 10
message: Bamboozled
details: You've been bamboozled
services.yaml` contains :
paths:
/logistic:
post:
description: Logistic
responses:
403:
description: Forbidden
content:
application/json:
schema:
$ref: '../common.yaml#/components/schemas/GenericError'
examples:
Bamboozled:
value:
$ref: '../common.yaml#/components/examples/Bamboozled/value'
In services.yaml, I would like to override the details field. I tried the following :
examples:
Bamboozled:
value:
$ref: '../common.yaml#/components/examples/Bamboozled/value'
details: my custom details
but this doesn't work, it will render :
{
"$ref":
"#/paths/~1logistic/post/responses/403/content/application~1json/examples/Bamboozled/value",
"details": "my custom details"
}
Is there a way I can override it correctly ?
Alternatively, can I reference only specific fields ? So I would have services.yaml something such as :
examples:
Bamboozled:
value:
$ref: '../common.yaml#/components/examples/Bamboozled/value/code'
$ref: '../common.yaml#/components/examples/Bamboozled/value/message'
details: my custom details
(this doesn't work)

Create swagger yaml for empty json array object

One of my objects in response is kept as empty. I am unable to create the swagger yaml for that part.
... <<something>>
,
"dummy": [
{}
],
... <<something>>
I have written below piece:
..... <<something>>
dummy:
$ref: '#/definitions/dummy'
..... <<something>>
dummy:
type: array
items:
$ref: "#/definitions/dumm"
dumm:
properties:
type: "object"
The error seen is "should be object" in front of the last line above.
How to resolve this?
The correct definition of your model would be:
dummy:
type: array
items:
$ref: "#/definitions/dumm"
dumm:
type: "object"
Do not use properties if the model does not have any of them.

How to define array-of-objects as parameter?

I am quite new to Swagger, so this might be a basic question.
I am able to create .yml file for an API which takes an array of integers as parameter, as follows:
Add samples
---
tags:
- MY API
parameters:
- name: my_id
in: path
type: integer
required: true
description: Some des
- name: body
in: body
schema:
id: add_samples
required:
- sample_ids
properties:
sample_ids:
type: array
items:
type: integer
description: A list of sample ids to be added
responses:
'200':
description: Added samples.
'400':
description: Error adding samples.
This is what I send to the above API and everything works fine:
{"sample_ids": [475690,475689,475688]}
Now, instead of an array of integers, if I want to use some complex object as parameter, how to do it?
E.g. If this is what I want to send:
{"sample_ids": [{
"sample_id": "7",
"some_prop": "123"
},
{
"sample_id": "17",
"some_prop": "134"
}]}
How should the .yml file look? I have tried something like this and it doesn't seem to work:
Add sample
---
tags:
- Samples API
models:
Sample:
id: Sample
properties:
sample_id:
type: string
default: ""
description: The id for this sample
some_prop:
type: integer
description: Some prop this sample
parameters:
- name: body
in: body
schema:
id: add_sample
required:
- sample_ids
properties:
samples:
type: array
description: A list of samples to be added
items:
$ref: Sample
responses:
'201':
description: Created a new sample with the provided parameters
'400':
description: SOME ERROR CODE
This one seems to work, mostly:
Add sample
---
tags:
- Samples API
models:
Sample:
id: Sample
properties:
sample_id:
type: string
default: ""
description: The id for this sample
some_prop:
type: integer
description: Some prop this sample
parameters:
- name: body
in: body
schema:
id: add_sample
required:
- sample_ids
properties:
samples:
type: array
description: A list of samples to be added
items:
$ref: Sample
responses:
'201':
description: Created a new sample with the provided parameters
'400':
description: SOME ERROR CODE
Now only problem is, in the Swagger UI, it is not showing member variables and their default values. Rather is showing it as null:
{
"samples": [
null
]
}

Resources