Skip to content

Instantly share code, notes, and snippets.

@vrialland
Created February 16, 2018 15:19
Show Gist options
  • Save vrialland/7031316121cb8e025dfca9dccfc226b1 to your computer and use it in GitHub Desktop.
Save vrialland/7031316121cb8e025dfca9dccfc226b1 to your computer and use it in GitHub Desktop.
Example OpenAPI 3 polymorphism
openapi: "3.0.0"
info:
version: 1.0.0
title: Vehicles
description: Polymorphism example
paths:
/vehicles:
get:
summary: List all vehicles
tags:
- vehicles
responses:
'200':
description: An paged array of vehicles
content:
application/json:
schema:
$ref: "#/components/schemas/Vehicle"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/vehicles/{id}:
get:
summary: Info for a specific vehicle
tags:
- vehicles
parameters:
- name: id
in: path
required: true
description: The id of vehicle to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/Car"
- $ref: "#/components/schemas/Plane"
discriminator:
propertyName: type
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
components:
schemas:
Vehicle:
type: object
required:
- id
- type
properties:
id:
type: integer
type:
type: string
model:
type: string
name:
type: string
discriminator:
propertyName: type
Car:
allOf:
- $ref: "#/components/schemas/Vehicle"
- type: object
properties:
type:
enum:
- car
has_4_wheel_drive:
type: boolean
Plane:
allOf:
- $ref: "#/components/schemas/Vehicle"
- type: object
properties:
type:
enum:
- plane
has_reactor:
type: boolean
nb_passengers:
type: integer
Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
@reece
Copy link

reece commented Feb 7, 2019

I'm struggling to validate against polymorphic models in the request body of a POST. I tried this:

  /vehicles:
    post:
      summary: Create new vehicle
      tags: ["vehicles"]
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: "#/components/schemas/Car"
                - $ref: "#/components/schemas/Plane"
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: "#/components/schemas/Car"
                  - $ref: "#/components/schemas/Plane"

Then, when I post

{
  "id": 0,
  "model": "string",
  "name": "string",
  "type": "Car"
}

connexion tells me that the post body doesn't validate against the schema.

Do you know of an example that demonstrates POST and GET of polymorphic models?

@jeroenvdm
Copy link

@reece,

I have the same issue. Trying to implement a polymorphic API using connexion and openapi 3.0. Have you made any progress?

Kind regards,

Jeroen

@third-bank-of-the-river

Same thing happens with me. :|

@simge-akosman
Copy link

simge-akosman commented Sep 4, 2023

      requestBody: 
        content: 
          application/json: 
            schema: 
              discriminator:
                propertyName: type
                mapping: 
                  Car:  "#/components/schemas/Car"
                  Plane: "#/components/schemas/Plane"
              anyOf: 
                - $ref: "#/components/schemas/Car"
                - $ref: "#/components/schemas/Plane"
      summary: List all vehicles
      tags:
        - vehicles
      responses:
        '200':
          description: An paged array of vehicles
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Vehicle"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error

i made some changes on your configuration. It's working now. 
@EmineKarakayali

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment