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

Check for incorrect discriminator use

Open adamaltman opened this issue 5 years ago • 3 comments

Is your feature request related to a problem? Please describe.

Yes: users need to get feedback about incorrect discriminator use.

Describe the solution you'd like

no-discriminator-property-in-object

Problem message:

The discriminator property {insert the property name} must exist in schemas {...names}.

Describe alternatives you've considered None.

Additional context Here is a minimal example of a problematic schema:

components:
  schemas:
    Problematic:
      type: object
      properties:
        data:
          discriminator:
            propertyName: objectType
          oneOf:
          - $ref: '#/components/schemas/A'
          - $ref: '#/components/schemas/B'
        objectType:
          type: string
    A:
      type: object
      properties:
        message:
          type: string
    B:
      type: object
      properties:
        message:
          type: string

Notice that objectType is defined in the Problematic schema and not in A and B, and this is the problem that it is not defined in A and B.

adamaltman avatar Sep 17 '20 15:09 adamaltman

I tried out the example and it does give an error already:

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

23 | application/json:
24 |   schema:
25 |     $ref: '#/components/schemas/Problematic'

Is that error not exactly like (or sufficiently close enough to) the one requested by this issue?

TylerRick avatar Aug 31 '23 02:08 TylerRick

@TylerRick you probably add example for this schema and this error was generated by the no-invalid-media-type-examples rule. So this is kind of different error. But thank you that you noticed that, because it can help and simplify implementation of proposed rule in future!

SmoliyY avatar Aug 31 '23 11:08 SmoliyY

Ah yes, you are right. I've been so focused on examples lately that I didn't notice this issue was not about an example rule... :smile:

TylerRick avatar Aug 31 '23 15:08 TylerRick