redocly-cli icon indicating copy to clipboard operation
redocly-cli copied to clipboard

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

Open matthias-m-fadi opened this issue 2 years ago • 2 comments

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 avatar Aug 15 '23 11:08 matthias-m-fadi

Hi @matthias-m-fadi,

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

tatomyr avatar Aug 15 '23 12:08 tatomyr

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 avatar Jan 03 '24 19:01 tatomyr