I have a bit of a unique situation where I want to use define a generic object as the return value of an endpoint in an OpenAPI yaml file, then define any number of objects that can be potential return values.
The generic object:
resource:
description: Resource objects appear in a JSON API document to represent resources.
type: object
required:
- type
- id
properties:
type:
$ref: "#/components/schemas/type"
id:
$ref: "#/components/schemas/id"
attributes:
type: object
relationships:
type: object
links:
$ref: "#/components/schemas/links"
meta:
$ref: "#/components/schemas/meta"
additionalProperties: false
One of the custom objects
CustomObjectDatum:
description: A custom object with fields that match the above resources object
type: object
required:
- type
- id
- attributes
properties:
type:
type: string
description: "[resource object type](https://jsonapi.org/format/#document-resource-object-identification)"
enum: [ type-identifier-for-deserialization ]
id:
$ref: ".#/components/schemas/id"
attributes:
$ref: "#/components/schemas/SomeAttributesObject"
links:
$ref: "./jsonapi.yaml#/components/schemas/links"
meta:
$ref: "./jsonapi.yaml#/components/schemas/meta"
additionalProperties: false
Elsewhere an endpoint is defined that returns a response object, of which “resource” is a field. Under the hood, the resource object will be populated with data from the CustomObjectDatum, and consumers will have access to both resource and CustomObjectDatum in order to deserialize correctly. The problem is, because CustomObjectDatum is defined in the schema but not referenced in the paths or in any of the other defined objects, it’s not being generated. Is there a way to force openapi-generator to generate this object the same way it does all the others?