mikunn / openapi-schema-to-json-schema

Converts OpenAPI Schema Object to JSON Schema
75 stars 6 forks source link

Inheritance (discriminator + allOf) is not correctly handled #30

Open norbjd opened 5 years ago

norbjd commented 5 years ago

Hello,

Consider this simple example with inheritance : a Pet is either a Dog or a Cat (petType is used for discrimination). Dogs and Cats have specific attributes :

This example can be represented by the following yaml (OpenAPI spec version 3). Endpoint /pets allows to create a Pet (either a Dog or a Cat).

openapi: 3.0.0

info:
  version: 1.0

paths:
  /pets:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'

components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - petType
      discriminator:
        propertyName: petType
        mapping:
          cat: '#/components/schemas/Cat'
          dog: '#/components/schemas/Dog'
      properties:
        id:
          type: string
        petType:
          type: string

    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            dogAttribute1:
              type: string

    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            catAttribute1:
              type: string

This is the result of openapi2schema -i /path/to/myfile.yaml (I used CLI wrapper here, but I'm sure the issue comes from the library) :

{
  "/pets": {
    "post": {
      "body": {
        "type": "object",
        "required": [
          "id",
          "petType"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "petType": {
            "type": "string"
          }
        },
        "$schema": "http://json-schema.org/draft-04/schema#"
      }
    }
  }
}

As you see, inheritance is not correctly handled (nothing about Dog or Cat here). I would have expected something like this instead :

{
  "/pets": {
    "post": {
      "body": {
        "type": "object",
        "oneOf": [
          {
            "properties": {
              "id": {
                "type": "string"
              },
              "petType": {
                "type": "string"
              },
              "dogAttribute1": {
                "type": "string"
              }
            },
            "required": [
              "id",
              "petType"
            ]
          },
          {
            "properties": {
              "id": {
                "type": "string"
              },
              "petType": {
                "type": "string"
              },
              "catAttribute1": {
                "type": "string"
              }
            },
            "required": [
              "id",
              "petType"
            ]
          }
        ],
        "$schema": "http://json-schema.org/draft-04/schema#"
      }
    }
  }
}

I don't know if this is easily feasible : I've seen that discriminator is not supported yet, so there is probably a reason. I would have liked to help, but my knowledge of JavaScript is close to zero :smile:

Anyway, thanks for this library.