OAI / OpenAPI-Specification

The OpenAPI Specification Repository
https://openapis.org
Apache License 2.0
28.91k stars 9.07k forks source link

Support for object types in discriminator #2731

Closed ramuklawjju closed 4 months ago

ramuklawjju commented 3 years ago

For discriminators, there should be a feature like propertyType which will enable us to control discriminators in a better way.

jdesrosiers commented 3 years ago

Can you be more specific? What would propertyType look like? What problem are you trying to solve?

karleinarb commented 2 years ago

Hello,

We have the same need of using boolean as discriminator. We could change our datastructure to use a string-value instead, but bool would be the most correct fit for our case and we do not want to change our ideal datastructure because of a missing feature in OAS. Until this is possible we will just have to drop using discriminators and tackle the cases discriminators could help us with in code, but it would be great to use OAS for this in the future.

Here is a simplified example of our case:

Example yaml

NewJob:
  oneOf:
  - $ref: '#/components/schemas/Job'
  - $ref: '#/components/schemas/UnmanagedJob'
  discriminator:
  propertyName: managed
  mapping:
    true: '#/components/schemas/Job'
    false: '#/components/schemas/UnmanagedJob'

Test request

{
  "managed": true
}

Error when using a validator

Bad Request: 'managed' should be equal to one of the allowed values: true, false.\n    at RequestValidator.discriminatorValidator

A solution could be

NewJob:
  oneOf:
  - $ref: '#/components/schemas/Job'
  - $ref: '#/components/schemas/UnmanagedJob'
  discriminator:
  propertyName: managed
  propertyType: boolean # Could be string, number or boolean. default string
  mapping:
    true: '#/components/schemas/Job'
    false: '#/components/schemas/UnmanagedJob'
karenetheridge commented 2 years ago
mapping:
  true: '#/components/schemas/Job'
  false: '#/components/schemas/UnmanagedJob'

This part isn't really possible because you can't represent booleans as object properties using the JSON document model -- property names can ONLY be strings.

radislav commented 2 years ago

This part isn't really possible because you can't represent booleans as object properties using the JSON document model -- property names can ONLY be strings.

That's why a feature like propertyType can help. It gives information about the type of the keys.

joergdw commented 11 months ago

+1 for this feature!

LasneF commented 9 months ago

I would rather go away of this and push any schema related feature to json schema rather than continue in forking json schema behaviour

then not sure you may be able to play with if then else https://json-schema.org/learn/miscellaneous-examples#conditional-validation-with-if-else

handrews commented 4 months ago

I'm going to close this as Moved to Moonwalk, specifically to:

because this would require a breaking change and generally more than we can do in 3.x. Please feel free to copy any salient points over to that discussion!