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

no-invalid-media-type-examples error for schema with oneOf (with discriminator) containing other schema with allOf #1232

Open
matthias-m-fadi opened this issue Aug 15, 2023 · 2 comments
Labels
governance Issues relating to problems with or requests for API governance/linting/decorating p2 question Further information is requested Type: Bug Something isn't working

Comments

@matthias-m-fadi
Copy link

Describe the bug

When defining a property that can be oneOf several schemas with a discriminator property and if one of the possible schemas is defined via allOf, then examples for the resulting schema fail validation due to a supposedly missing property on the contained allOf-Schema, even though it exists.

Apart from the error being incorrect, the reported error location is also very far from the source of it and it was difficult tracking this down to the actual cause in a complex nested schema.

To Reproduce
Steps to reproduce the behavior:

  1. Given this redocly.yaml file
extends:
  - recommended
rules:
  info-license: off
  operation-operationId: off
  operation-4xx-response: off
  no-invalid-media-type-examples: error
  1. And this OpenAPI file(s)
openapi: 3.0.2
info:
  title: API Title
  version: '1.0'

servers:
  - url: https://api.server.test/v1
security:
  - bearer-jwt: []
paths:
  /foo:
    get:
      description: ""
      summary: "a"
      responses:
        '200':
          description: ""
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
              example:
                choice:
                  kind: Item

components:
  securitySchemes:
    bearer-jwt:
      type: http
      scheme: bearer
      bearerFormat: JWT
  schemas:
    Foo:
      type: object
      properties:
        choice:
          oneOf:
            # there can be more entries, makes no difference
            - $ref: "#/components/schemas/Item"
          discriminator:
            propertyName: kind

    Item:
      allOf:
        # there can be more schemas listed here. It doesn't matter
        - type: object
          properties:
            kind:
              type: string
          required:
            -  kind
  1. Run this command with these arguments... redocly ...
    redocly lint minimal_error.yaml I suppose (I'm using npx @redocly/cli lint minimal_error.yaml)

  2. See error

Expected behavior
No error. The reference schema has the required discriminator property.

Logs
This is the actual output:

[1] minimal_error.yaml:21:17 at #/paths/~1foo/get/responses/200/content/application~1json/schema

Example validation errored: discriminator: oneOf subschemas (or referenced schemas) must have "properties/kind" or match mapping.

19 | application/json:
20 |   schema:
21 |     $ref: '#/components/schemas/Foo'
22 |   example:
23 |     choice:

referenced from minimal_error.yaml:20:15 at #/paths/~1foo/get/responses/200/content/application~1json 

Error was generated by the no-invalid-media-type-examples rule.

Redocly Version(s)
1.0.2

Node.js Version(s)
v18.17.0

Additional context

@matthias-m-fadi matthias-m-fadi added the Type: Bug Something isn't working label Aug 15, 2023
@tatomyr
Copy link
Contributor

tatomyr commented Aug 15, 2023

Hi @matthias-m-fadi,

This is a bug indeed. Thank you for reporting the issue!

@tatomyr tatomyr added the p2 label Aug 15, 2023
@lornajane lornajane added the governance Issues relating to problems with or requests for API governance/linting/decorating label Nov 17, 2023
@tatomyr
Copy link
Contributor

tatomyr commented Jan 3, 2024

Hi @matthias-m-fadi! Have you tried adding the mapping to the discriminator as the error message suggests?
The error seems to be gone once I've added the mapping:

discriminator:
  propertyName: kind
  mapping: 
    Item: "#/components/schemas/Item"

See the guide on discriminator usage for more details: https://redocly.com/docs/resources/discriminator/

@tatomyr tatomyr added the question Further information is requested label Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
governance Issues relating to problems with or requests for API governance/linting/decorating p2 question Further information is requested Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants