schema array in swagger - swagger

I have some problem with swagger. When I think I understand how it works, there's always something that doesn't work
What's wrong in thoses line
responses:
'200':
allOf:
- $ref: '../index.yaml#/components/responses/200Ok'
content:
application/json:
schema:
allOf:
- $ref: '../index.yaml#/components/schemas/Pagination'
properties:
data:
type: array
items:
schema:
$ref: '../index.yaml#/components/schemas/Client'
The "data" property should be an array of the schema type given in the $ref, but this is the result
"data": [
null
]
EDIT
Ok, it seems that the right way is tu put the $ref directly under the items key, my problem was the use of a reserved key "status"
So, how can I use a reserved key in a object schema?
EDIT
in my Client schema I put the property status two times, I didn't see that it was already there, so when I changed the property name it worked and I was thinking that maybe "status" was a reserved keyword.

You are almost there. There are two issues:
1) You can't have allOf directly under a response code. You can $ref the whole response definition though.
2) You don't need schema under items.
Also, while putting allOf alongside other keywords is perfectly fine, some tools may like it better if all schemas being combined are listed inside allOf.
Try this version:
responses:
'200':
description: OK
content:
application/json:
schema:
allOf:
- $ref: '../index.yaml#/components/schemas/Pagination'
- properties:
data:
type: array
items:
$ref: '../index.yaml#/components/schemas/Client'

Related

OpenAPI multiple types inside an array

I'm having trouble defining a reusable schema component using OpenAPI 3 which would allow for an array that contains multiple types. Each item type inherits from the same parent class but has specific child properties. This seems to work alright in the model view on SwaggerHub but the example view doesn't show the data correctly.
TLDR; Is there a way to define an array containing different object types in OpenAPI 3?
Response:
allOf:
- $ref: '#/components/schemas/BaseResponse'
- type: object
title: A full response
required:
- things
properties:
things:
type: array
items:
anyOf:
- $ref: '#/components/schemas/ItemOne'
- $ref: '#/components/schemas/ItemTwo'
- $ref: '#/components/schemas/ItemThree'
Your spec is correct. It's just that example rendering for oneOf and anyOf schemas is not yet supported in Swagger UI. You can track this issue for status updates:
Multiple responses using oneOf attribute do not appear in UI
The workaround is to manually add an example alongside the oneOf/anyOf schema or to the parent schema:
things:
type: array
items:
anyOf:
- $ref: '#/components/schemas/ItemOne'
- $ref: '#/components/schemas/ItemTwo'
- $ref: '#/components/schemas/ItemThree'
# Note that array example is on the same
# level as `type: array`
example:
- foo: bar # Example of ItemOne
baz: qux
- "Hello, world" # Example of ItemTwo
- [4, 8, 15, 16, 23, 42] # Example of ItemThree

When should I use $ref versus the object id in Open API

I'm working on a schema in OpenAPI with Swagger, and I'm not sure if I'm misusing the $ref element. I have a User model and a Project model, similar to something like
User:
type: object
properties:
id:
type: string
format: uuid
name:
type: string
...
Project:
type: object
properties:
id:
type: string
user_id:
$ref: "#/components/schemas/User"
...
I don't see much in the Open API Spec documentation as to what the $ref element is specifically, but in the JSON Schema documentation—of which Open API extends the $ref element—I've found the following description of the item:
The easiest way to describe $ref is that it gets logically replaced with the thing that it points to.
In the case above, I just want to refer to the user who posted a project. It seems unnecessary to essentially include all of the information about the User in the Project model if that is what it's doing. Would it be better practice to just have a string element of the uuid of the user_id? Or is it correct as is? Is it more common to name the field user rather than user_id if that is the case?
Edit:
I realize the heart of what was bothering me is if there were to be recursive references. If a User has an array of $ref to Projects, but also a Project has an array of $ref to Users, the replacement (if that's what it's doing) would infinitely embed each model in the other. I assume this wouldn't happen in practice, assuming that a $ref is just a pointer to the Model?
In your example it might make sense to extract the userId definition into its own schema (assuming it is the userId only that appears, not an entire User object), then it is clearer what's going on:
components:
schemas:
User:
type: object
properties:
id:
$ref: '#/components/schemas/userId'
name:
type: string
...
Project:
type: object
properties:
id:
type: string
user_id:
$ref: "#/components/schemas/userId"
...
userId:
type: string
format: uuid
But there is nothing stopping you creating a direct $ref to #/components/schemas/User/properties/id instead, as long as what is pointed to is a valid OpenAPI schemaObject.
Circular references are allowed by the JSON Reference and OpenAPI specifications, so your analogy of a pointer is a sound one.

How to exclude property from swagger definition

I'm using Swagger online editor. I create some model in definition
Object:
type: object
properties:
id:
type: integer
format: int32
name:
type: string
some_variable_to_exclude:
type: string
This is complete model and i use it in different responses. But in one of they i want my model dont present property "some_variable_to_exclude". How can i exclude it ? Is it possible ?
Okay, maybe not exclude, maybe some comment near property, but only for that response.
In order to extend the Swagger Schema with custom objects, the field must begin with x-
See more here: http://swagger.io/specification/#vendorExtensions
For example, swagger-node uses x-swagger-router-controller to specify which controller to use for a particular API path. But you can use it for whatever you need, e.g., x-custom-property
Create another definition without field you don't need
Create definition without field you don't need, like
components:
schemas:
Object:
type: object
properties:
id:
type: integer
format: int32
name:
type: string
then, on describe new full scheme, write this:
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/Object'
- properties:
some_variable_to_exclude:
type: string

Can I specify the response directly in Swagger 2.0?

I am defining my response as per:
responses:
200:
description: 'An authProvider object'
schema:
type: 'array'
items:
$ref: '#/definitions/AuthProvider'
Is it possible for me to directly attach another field or 2 in the response? I would like to add something that I don't want to put into AuthProvider
Whenever $ref is used, it's basically using JSON Reference. By definition (see linked spec), anything defined within the same object as the reference itself is ignored. As such, it is not possible to directly add additional properties alongside with the $ref definition.
That said, you can use composition to 'combine' two objects into one. This is done using the allOf JSON Schema directive. A simple example for such a definition would look like this:
allOf:
-
$ref: "..."
-
properties:
property1:
type: "string"
property2:
type: "boolean"
Note 1: The various Swagger tools out there may not fully support composition at the moment.
Note 2: As always, it's normally better to externalize the definition (even if used only once) rather than being defined inline.

Inheritance in swagger-editor throwing an error

Using swagger editor and getting a validation error when I try to inherit from a model contains an array. I am assuming that my definition is screwed up as I am new to swagger. But I do know that there is still work to be done to support swagger 2.0 in the editor. Not looking to point out a bug or deficiency in the editor, just want to know if my schema is valid.
This works in the editor (from simple petstore example):
definitions:
pet:
properties:
name:
type: string
newPet:
allOf:
- $ref: '#/definitions/pet'
However I want to define pet as an array:
definitions:
pet:
type: array
items:
type: string
newPet:
allOf:
- $ref: '#/definitions/pet'
Technically speaking, that's not inheritance, that's composition. There's no hierarchical relation between newPet and pet. For a inheritance, the definitions must both be objects, and there has to be a discriminator definition.
As far as composition goes, that looks like a valid definition.
Here is an example: https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/yaml/petstore-expanded.yaml
So it looks like this:
definitions:
Pet:
allOf:
- $ref: '#/definitions/NewPet'
required:
- id
properties:
id:
type: integer
format: int64
NewPet:
required:
- name
properties:
name:
type: string
tag:
type: string

Resources