OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

Support for object types in discriminator

Open ramuklawjju opened this issue 4 years ago • 4 comments

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

ramuklawjju avatar Sep 28 '21 07:09 ramuklawjju

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

jdesrosiers avatar Sep 28 '21 16:09 jdesrosiers

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'

karleinarb avatar Nov 30 '21 10:11 karleinarb

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.

karenetheridge avatar Dec 02 '21 04:12 karenetheridge

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.

radislav avatar Mar 31 '22 09:03 radislav

+1 for this feature!

joergdw avatar Oct 27 '23 14:10 joergdw

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

LasneF avatar Dec 18 '23 17:12 LasneF