requestBody example in polymorphic schema (with `discriminator`) causes `no-invalid-media-type-examples` warning
See original GitHub issueDescribe the bug
requestBody example in polymorphic schema (with discriminator
) causes no-invalid-media-type-examples
warning
To Reproduce Steps to reproduce the behavior:
- Given this
.redocly.yaml
file Default rules - 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": {
}
}
}
}
}
- Run this command with these arguments…
openapi ...
openapi lint api.json --format=stylish
- 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:
- Created 3 years ago
- Reactions:9
- Comments:12 (10 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We provide a resolver. I’ll show you.
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.