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.

requestBody example in polymorphic schema (with `discriminator`) causes `no-invalid-media-type-examples` warning

See original GitHub issue

Describe the bug requestBody example in polymorphic schema (with discriminator) causes no-invalid-media-type-examples warning

To Reproduce Steps to reproduce the behavior:

  1. Given this .redocly.yaml file Default rules
  2. And this OpenAPI file(s)
{
  "openapi": "3.0.2",
  "info": {
    "title": "API",
    "version": "1.0"
  },
  "servers": [
    {
      "url": "//petstore.swagger.io/v2",
      "description": "Default server"
    }
  ],
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "properties": {
          "petType": {
            "type": "string",
            "enum": [
              "Dog",
              "Cat"
            ]
          }
        }
      },
      "Dog": {
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Pet"
          },
          {
            "type": "object",
            "properties": {
              "packSize": {
                "type": "integer"
              }
            }
          }
        ]
      },
      "Cat": {
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Pet"
          },
          {
            "type": "object",
            "properties": {
              "huntingSkill": {
                "type": "string"
              }
            }
          }
        ]
      },
      "House": {
        "type": "object",
        "properties": {
          "address": {
            "type": "string"
          },
          "pet": {
            "type": "object",
            "oneOf": [
              {
                "$ref": "#/components/schemas/Dog"
              },
              {
                "$ref": "#/components/schemas/Cat"
              }
            ],
            "discriminator": {
              "propertyName": "petType",
              "mapping": {
                "dog": "#/components/schemas/Dog",
                "cat": "#/components/schemas/Cat"
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/house": {
      "post": {
        "operationId": "newHouse",
        "summary": "New house",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/House"
              },
              "examples": {
                "HouseWithDog": {
                  "value": {
                    "pet": {
                      "petType": "dog",
                      "packSize": 1
                    },
                    "address": "abc"
                  }
                },
                "HouseWithCat": {
                  "value": {
                    "pet": {
                      "petType": "cat",
                      "huntingSkill": "adventurous"
                    },
                    "address": "abc"
                  }
                }
              }
            }
          }
        },
        "responses": {

        }
      }
    }
  }
}

  1. Run this command with these arguments… openapi ... openapi lint api.json --format=stylish
  2. See error
No configurations were defined in extends -- using built in recommended configuration by default.

validating openapi/api.json...
openapi/api.json:
  3:11    warning  info-description                Info object should contain `description` field.
  3:11    warning  info-license                    Info object should contain `license` field.
  103:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `packSize`.
  102:34  warning  no-invalid-media-type-examples  Example value must conform to the schema: `petType` property should be equal to one of the allowed values "Dog", "Cat".
  102:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `petType`.
  103:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `packSize`.
  102:34  warning  no-invalid-media-type-examples  Example value must conform to the schema: `petType` property should be equal to one of the allowed values "Dog", "Cat".
  102:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `petType`.
  103:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `packSize`.
  101:28  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should match exactly one schema in oneOf.
  112:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `huntingSkill`.
  111:34  warning  no-invalid-media-type-examples  Example value must conform to the schema: `petType` property should be equal to one of the allowed values "Dog", "Cat".
  111:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `petType`.
  112:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `huntingSkill`.
  112:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `huntingSkill`.
  111:34  warning  no-invalid-media-type-examples  Example value must conform to the schema: `petType` property should be equal to one of the allowed values "Dog", "Cat".
  111:23  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should NOT have additional properties `petType`.
  110:28  warning  no-invalid-media-type-examples  Example value must conform to the schema: `pet` property should match exactly one schema in oneOf.
  121:9   warning  operation-2xx-response          Operation must have at least one `2xx` response.

openapi/api.json: validated in 34ms

Woohoo! Your OpenAPI definition is valid. 🎉
You have 19 warnings.

Expected behavior There is no no-invalid-media-type-examples warning

Logs N/A

OpenAPI definition 3.0.2

openapi-cli Version(s) v1.0.0-beta.25

Node.js Version(s) v12.18.3

Additional context N/A

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
RomanHotsiycommented, Oct 21, 2021

The reason for this is that validator does not resolve references inside definition. I guess we need to resolve all references before we pass definition to validator.

We provide a resolver. I’ll show you.

1reaction
RomanHotsiycommented, Oct 21, 2021

https://github.com/ajv-validator/ajv/blob/325ac9a6396baed3eaf588ee80aed3b1f23d12dd/lib/vocabularies/discriminator/index.ts#L30

Here’s why. @slavikbez let’s try to fix it in our AJV fork and then we can contribute back if it’s stable enough.

Read more comments on GitHub >

github_iconTop Results From Across the Web

requestBody example in polymorphic schema (with ... - GitHub
Describe the bug requestBody example in polymorphic schema (with discriminator) causes no-invalid-media-type-examples warning To Reproduce ...
Read more >
How to use the OpenAPI discriminator - Redocly
When an API can return two or more different types of objects (aka polymorphism), use oneOf or anyOf to describe those schemas (a...
Read more >
Inheritance and Polymorphism - Swagger
In your API, you may have model schemas that share common properties. ... In our example, the discriminator points to the objectType property...
Read more >
"discriminator" in polymorphism, OpenAPI 2.0 (Swagger 2.0)
According to this google group, discriminator is used on top of the allOf property and it is defined in the super type for...
Read more >
Best approach to define array with polymorphic types
paths: /pets: post: requestBody: content: application/json: schema: type: array items: $ref: '#/components/schemas/Pet' responses: '200': ...
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