redocly-cli
redocly-cli copied to clipboard
no-invalid-media-type-examples error for schema with oneOf (with discriminator) containing other schema with allOf
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:
- Given this
redocly.yamlfile
extends:
- recommended
rules:
info-license: off
operation-operationId: off
operation-4xx-response: off
no-invalid-media-type-examples: error
- 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
-
Run this command with these arguments...
redocly ...redocly lint minimal_error.yamlI suppose (I'm usingnpx @redocly/cli lint minimal_error.yaml) -
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
Hi @matthias-m-fadi,
This is a bug indeed. Thank you for reporting the issue!
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/