Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erro in schema oneOf (discriminator) #458

Closed
pmacaubas opened this issue Nov 13, 2020 · 2 comments
Closed

Erro in schema oneOf (discriminator) #458

pmacaubas opened this issue Nov 13, 2020 · 2 comments

Comments

@pmacaubas
Copy link

pmacaubas commented Nov 13, 2020

Describe the bug

I tried to use oneOf and the error happened:

{
"message": "request.body should match exactly one schema in oneOf",
"errors": [
{
"path": ".body",
"message": "should match exactly one schema in oneOf",
"errorCode": "oneOf.openapi.validation"
}
]
}

To Reproduce

app.yaml

This example is the same code that is in OpenApi Specification.

openapi: '3.0.3'
info:
  version: 1.0.0
  title: Swagger 
  description: XXXXX
  termsOfService: http://swagger.io/terms/
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
  #- url: /v1
  - url: /v1
paths:
  /pets:
   post:
      description: Creates a new pet in the store.
      operationId: createPet
      x-eov-operation-id: pets#create ## overrides operationId
      x-eov-operation-handler: api/controllers/pets
      requestBody:
        description: Pet to add to the store
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
              discriminator:
                propertyName: pet_type
      responses:
        '200':
          description: Updated
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    Pet:
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
    Dog:     # "Dog" is a value for the pet_type property (the discriminator value)
      allOf: # Combines the main `Pet` schema with `Dog`-specific properties 
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: boolean
            breed:
              type: string
              enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:     # "Cat" is a value for the pet_type property (the discriminator value)
      allOf: # Combines the main `Pet` schema with `Cat`-specific properties 
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            hunts:
              type: boolean
            age:
              type: integer

Request

curl --location --request POST 'http://localhost:10010/v1/pets' \
--header 'Content-Type: application/json' \
--data-raw '{
  "pet_type": "Cat",
  "hunts": true,
  "age": 3
}'

Response ERROR

{
    "message": "request.body should match exactly one schema in oneOf",
    "errors": [
        {
            "path": ".body",
            "message": "should match exactly one schema in oneOf",
            "errorCode": "oneOf.openapi.validation"
        }
    ]
}

Actual behavior

As listed above, the error happened and the schema doesn't validated, because i used oneOf :

This example is the same code that is in OpenApi Specification.

requestBody:
        description: Pet to add to the store
        required: true
        content:
          application/json:
            schema:
              oneOf:
               - $ref: '#/components/schemas/Cat'
               - $ref: '#/components/schemas/Dog'
              discriminator:
                propertyName: pet_type

Expected behavior

I behavior expected is the same the specification for oneOf step:
https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not

In the OpenApi Specification the body bellow is valid :

{
  "pet_type": "Cat",
  "age": 3
}
{
  "pet_type": "Dog",
  "bark": true
}
{
  "pet_type": "Dog",
  "bark": false,
  "breed": "Dingo"
}

Examples and context

This example is the same code that is in OpenApi Specification.
https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not

@cdimascio
Copy link
Owner

@pmacaubas thanks for the issue. discriminator support is lacking. i have put together a PR that will handle top level discriminators with oneOf and anyOf. I'll get this merged in soon. will love to get your thoughts

cdimascio added a commit that referenced this issue Nov 15, 2020
* chore: update change log

* feat: add top level discriminator support #458

* validate one of

* continue if no discriminator option available found
@cdimascio cdimascio changed the title Erro in schema oneOf Erro in schema oneOf (discriminator) Nov 15, 2020
@cdimascio
Copy link
Owner

@pmacaubas initial discriminator support is available in v4.7.0. all use cases are not yet support, but this one is.
please give it a try. as use cases come up, please file them and i'll prioritize
thanks!

here are some examples:

ex1st pushed a commit to ex1st/express-openapi-validator that referenced this issue Dec 9, 2020
…imascio#461)

* chore: update change log

* feat: add top level discriminator support cdimascio#458

* validate one of

* continue if no discriminator option available found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants