question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Discriminator field (unnecessarily) required

See original GitHub issue

Is it possible to remove the restriction of making the field that acts as discriminator required (and assume that when not returned, the object is just of the ‘base’ type defined in the schema)?

i.e (using the Pet example from the docs)

Schema:

(...)
properties: 
    pets: 
       type: array
       items:
           type: Pet

Payload:

{
   pets: [
   {
      name: "Some generic pet" 
   },
   {
      @type: Dog
      name: "Some dog"
      packSize: ...
   },
   {
      @type: Cat
      name: "Some cat"
      huntingSkill: ...
   },
   {
       name: "Some other generic pet"
   }
}

The first and third items are just of type “Pet” which would be explicit from the schema of the “pets” field. “Pet” would therefore define the @type field but it won’t be required. We use schema.org and rely on polymorphic schemas extensively but we currently only return a type discriminator when is not obvious from the schema (i.e when returning a subtype). Unless I misunderstand it, adhering to the spec would require us to return @type everywhere which would significantly increase payload size (and thus would prob be a non-starter).

Let me know if you have any thoughts or reasons to not go this route.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
handrewscommented, Jul 24, 2018

@Relequestual discriminator does not fit well with other aspects of JSON Schema- I looked into it while we considered if/then/else and again during the whole unevaluatedProperties saga. I’d have to do some digging to explain exactly why.

0reactions
tmtroncommented, Sep 23, 2020

another use-case for an optional discriminator is API-evolution.

i.e. we may have an API that only supports one type (e.g. Dogs) and thus has no discriminator But later we need to add support for Cats

Then we can change the API in a backwards compatible way

  • original Dog objects (without discriminator will still work)
  • new Dog object with discriminator Dog will now also work
  • new Cat objects must have a discriminator
Read more comments on GitHub >

github_iconTop Results From Across the Web

_t unnecessary discriminator mongodb c# driver - Stack ...
The c# mongodb driver has been adding a "_t" property in all documents and all collections of my application, even though it isn't...
Read more >
required-discriminator-field - APIMatic Documentation
If a discriminator is set for a model, it must have a corresponding discriminator field/property defined in its fields. Property, Value. Severity, Error....
Read more >
Pix2pix Utilizing the Deep Learning Reference Stack - 01.org
Training a cGAN is difficult and convoluted, and requires several steps. The discriminator and the generator cannot be trained at the same ...
Read more >
Are mainstream pre-trained models useful as discriminators?
Another reason, I suspect, is that the discriminator's job is quite specialized and requires it to be dynamic; i.e., it has to be...
Read more >
Advertising S-BFD Discriminators in BGP - IETF
It is suggested to use BGP to distribute BFD discriminator information. ... BFD discriminators are also required to create an S-BFD session.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found