openapi-core icon indicating copy to clipboard operation
openapi-core copied to clipboard

Does openapi-core include support for discriminator

Open Origin1227 opened this issue 5 years ago • 4 comments

Hi , Does open-api core include support for discriminator i tried it but didn't seem to work. https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#discriminatorObject if not any hints on how i could include it in my project


 components:
  requestBodies:
    object:
      content:
        application/json:
          schema:
            oneOf:
              - $ref: '#/components/schemas/user'
              - $ref: '#/components/schemas/admin'
            discriminator:
              propertyName: item_type
  schemas:
    user:
      type: object
    admin:
      type: object

what i need is when the item_type is user in the request body it should be validated based on user schema and similarly for admin

Origin1227 avatar Jan 07 '20 05:01 Origin1227

Discriminator is not implemented per se.

But it shouldn't be a problem because if you use it only with propertyName it

act as a "hint" to shortcut validation and selection of the matching schema which may be a costly operation, depending on the complexity of the schema

p1c2u avatar Jan 13 '20 12:01 p1c2u

This is not working I am getting the error:

not valid for schema of type object: <Object
...
<Object ...> is valid under each of [<Object schema>, <Object schema>]

It seems to me that polymorphism support via discriminator should be supported as it is a key concept for many APIs.

snippit of schema:

    Resource:
      description: |
        A resource is a container for requirements. It allows the developer do define requirements for the current project
         or external things and reference them separately.
      type: object
      required:
        - name
      properties:
        name:
          type: string
        description:
          type: string
        requirements:
          type: array
          items:
            oneOf:
              - $ref: "apt_spec.yml#/components/schemas/AptRequirement"
              - $ref: "yum_spec.yml#/components/schemas/YumRequirement"
            discriminator:
              propertyName: requirementType


example data:
  • name: python3 requirementType: YumRequirement resolveDependencies: False
  • name: python3 requirementType: AptRequirement resolveDependencies: False

dingus9 avatar May 25 '21 16:05 dingus9

See https://github.com/p1c2u/openapi-core/issues/257#issuecomment-964053231 for a workaround.

ngnpope avatar Nov 09 '21 11:11 ngnpope

dingus9 is this issue still active ?

jaswantcoder avatar Oct 17 '23 20:10 jaswantcoder