Generic wrapper for response: questions about combining oneOf and allOf to mimick json-api
See original GitHub issueI have been using OpenAPI (v3) for my own project, learning it while doing so my problem might be just an oversight or a lack of practice with Swagger/OAS. I have been trying to get all my API response to issue a standard top-level wrapper, mimicking JSON-API specs (not full compliance yet) with “data”, “meta” and “errors”. I used to have the properties defined directly in the response schema, eg
paths:
[...]
responses:
[...]
schema:
properties:
data:
type: string # whatever payload
meta:
type: string # whatever payload
but it means a lot of repetitions and no obvious semantics.
So I created a proper schema for the top-level wrappers and, using model composition with the allOf
keyword (and a bit of playing around), I got a more meaningful output:
components:
schemas:
v1.JsonApi:
properties:
data:
description: ''
type: object
meta:
description: ''
type: object
errors:
description: ''
type: array
items:
$ref: '#/components/schemas/ApiProblem'
[...]
paths:
[...]
responses:
[...]
schema:
allOf:
- $ref: '#/components/schemas/v1.JsonApi'
- properties:
data:
type: string # whatever payload
meta:
type: string # whatever payload
The combination makes clear the output is of a JSONAPI schema (and will be validated off it) then ‘overrides’ the properties with the local paylod. So far so good.
But I still get the errors
property as part of the JSONAPI schema; as with the specs, both data+meta
and errors
top-level properties should be mutually exclusive.
Finally, I tried to redefined the JSONAPI schema using the oneOf
keywords to separate the two groups:
v3.JsonApi:
description: A better exclusion of the two groups of properties, usinf oneOf.
oneOf:
- properties:
data:
description: ''
type: object
meta:
description: ''
type: object
- properties:
errors:
description: ''
type: array
items:
$ref: '#/components/schemas/ApiProblem'
type: object
paths:
[...]
responses:
[...]
schema:
allOf:
- $ref: '#/components/schemas/v3.JsonApi'
- properties:
data:
type: string # whatever payload
meta:
type: string # whatever payload
But this time, it is not working: it is valid but the output is wrong (in swaggerUI or Hub, both in example and model).
So, three questions really:
- Did I miss something is tems of using json-api within OAS? I have checked both GitHub and SO, nothing pertinent.
- Am I using
allOf
properly when combining two schemas to override some of the properties or is this more of an exploit or side effect? - Am I doing something wrong when trying to combine a
allOf
(in response) with aoneOf
(in schema)? Is it just a rendering issue with SwaggerHub or a limitation of the OAS?
I’ve created a wee specs on SwaggerHub if you want to play around.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
I found @vanch3d’s original post extremely useful, and tried to solve why the example API data would show empty instead of the examples provided. I think it’s because listed above
data
was set toobject
but should bearray
.Example:
Then, when viewing the generated swagger docs, you can see example data showing in the 200 response preview:
When left to
object
fordata
andmeta
, the example shows:Given that this is well over a year old with no further comment, I’m declaring it answered.