question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect example validation with oneOf and discriminator

See original GitHub issue

Describe the bug When example of object defined as oneOf with discriminator is validated the discriminator.propertyName is not used to validate the example against specific schema from the oneOf array.

To Reproduce

  1. Given this OpenAPI/AsyncAPI document
openapi: '3.0.2'
info:
  title: API Title
  version: '1.0'
servers:
  - url: https://api.server.test/v1
paths:
  /test:
    get:
      operationId: getTest

      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  Items:
                    items:
                      $ref: '#/components/schemas/Item'
                    type: array
                required:
                  - Items
                type: object
              example:
                Items:
                  - Type: Standard
                    Name: test1
                    Id: 1
                  - Type: Extra
                    Name: test2
                    Id: 2
          description: Success

components:
  schemas:
    Item:
      discriminator:
        mapping:
          Standard: '#/components/schemas/Item/oneOf/1'
          Extra: '#/components/schemas/Item/oneOf/0'
        propertyName: Type
      oneOf:
        - properties:
            Name:
              type: string
            Type:
              type: string
            Id:
              type: number
          required:
            - Type
            - Id
            - Name
          type: object
        - properties:
            Name:
              type: string
            Type:
              type: string
            Id:
              type: number
            Extra:
              type: string
          required:
            - Type
            - Id
            - Name
            - Extra
          type: object
      required:
        - Type
  1. Run this CLI command spectral lint the_document.yml
  2. No error is produced even though the example response is not valid. Second element in the Items array is not valid according to schema definition for objects with Type=Extra

Expected behavior I expect spectral to consider discriminator definition when validating schemas with oneOf definition

Environment:

  • Library version: 5.5.0

Additional context This is even worse in situations when you meant to specify an example for the first schema in oneOf array but you get an error that your example is not valid according to the second schema in oneOf array. This happens because spectral apparently ignores discriminator definition and just validates against all of the schema definitions in oneOf array until it find one that is valid. Otherwise it generates error resulting from the validation of the last schema in the oneOf array. This leads to very confusing error messages. This is evident when we slightly edit the document:

openapi: '3.0.2'
info:
  title: API Title
  version: '1.0'
servers:
  - url: https://api.server.test/v1
paths:
  /test:
    get:
      operationId: getTest

      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  Items:
                    items:
                      $ref: '#/components/schemas/Item'
                    type: array
                required:
                  - Items
                type: object
              example:
                Items:
                  - Type: Standard
                    Name: null
                    Id: 1
                  - Type: Extra
                    Name: test2
                    Id: 2
          description: Success

components:
  schemas:
    Item:
      discriminator:
        mapping:
          Standard: '#/components/schemas/Item/oneOf/1'
          Extra: '#/components/schemas/Item/oneOf/0'
        propertyName: Type
      oneOf:
        - properties:
            Name:
              type: string
            Type:
              type: string
            Id:
              type: number
          required:
            - Type
            - Id
            - Name
          type: object
        - properties:
            Name:
              type: string
              nullable: true
            Type:
              type: string
            Id:
              type: number
            Extra:
              type: string
          required:
            - Type
            - Id
            - Name
            - Extra
          type: object
      required:
        - Type

Linting this schema generates error: 27:21 error oas3-valid-oas-content-example `0` property should have required property `Extra` . As previously explained this error message is in my opinion wrong and can be very hard to decipher in complex schemas/examples. I would expect error message saying that Name property can’t be null since the example should have been validated against the Standard schema only.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
philsturgeoncommented, Sep 29, 2020

We don’t support discriminator object, and your usage of it with oneOf is pretty wild. I don’t think I’ve ever seen anything like that!

We’ve got an issue for tracking subpar error messages coming out of AJV where we try and catch specific use cases and turn them into something more useful, and the 0 property (first item in the array) is already in there, so follow #1072 for an update on when that error message is improved.

0reactions
philsturgeoncommented, Mar 15, 2021

Maybe in the future, OpenAPI has a proposal for alternativeSchemas which would allow XML Schema and maybe Protobuf and so that would be the most logical place for JTD to try and slow in if anyone is interested.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Discriminator does not seem to be evaluated - Bitbucket
My first try: Use allOf for Inheritance, apply a discriminator in the super-type and use oneOf in the references, where this supertype is ......
Read more >
oneOf, anyOf, allOf, not - Swagger
The example above shows how to validate the request body in the “update” operation (PATCH) ... For more clearness, oneOf is also used...
Read more >
How to use the OpenAPI discriminator - Redocly
Use oneOf when it can only be valid against one of the schemas. ... The examples below with the vehicles would require anyOf...
Read more >
Documentation for the java Generator
Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) ......
Read more >
OpenAPI v3 discriminator definitions are letting invalid ...
0.0 (online Swagger validators pass it with no issues), run-time validations are saying invalid requests are valid and letting them through to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found