Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discriminator value and inheritance #1656

Open
trajano opened this issue Dec 11, 2024 · 0 comments
Open

Discriminator value and inheritance #1656

trajano opened this issue Dec 11, 2024 · 0 comments

Comments

@trajano
Copy link

trajano commented Dec 11, 2024

This is a specific use case but posing it as a discussion (I do recommend we have Discussion enabled on the project to clarify ideas)

Assuming we this schema

{
  "$schema" : "https://json-schema.org/draft/2020-12/schema",
  "title" : "Question",
  "type" : "object",
  "oneOf" : [ {
    "$ref" : "./MultipleChoiceQuestion.schema.json"
  }, {
    "$ref" : "./SimpleAnswerQuestion.schema.json"
  } ],
  "additionalProperties": false,
  "javaTypeDiscriminatorProperty": "type"
}

{
  "$schema" : "https://json-schema.org/draft/2020-12/schema",
  "title" : "QuestionBase",
  "type" : "object",
  "requiredProperties": [ "type", "id" ],
  "additionalProperties": false,
  "properties": {
     "type": { "type": "string" },
     "id": { "type": "string" }
  }
}

{
  "$schema" : "https://json-schema.org/draft/2020-12/schema",
  "title" : "MultipleChoiceQuestion",
  "allOf": [
    { "$ref": "./QuestionBase.schema.json" },
    {
      "type" : "object",
      "requiredProperties": [ "choices" ],
      "additionalProperties": false,
      "properties": {
         "type": { "type": "string", "enum": [ "multiple_choice" ] },
         "choices": { "type": "array", "items": { "type": "string" } },
      }
   }
 ]
}

We should get something like this

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME, 
    include = JsonTypeInfo.As.EXISTING_PROPERTY,
    property = "type"
    )
@JsonSubTypes({
  @JsonSubTypes.Type(value = MultipleChoiceQuestion.class, name = "multiple_choice"),
  @JsonSubTypes.Type(value = SimpleAnswerQuestion.class, name = "simple_answer")
})
// note interface if it detects there's nothing in the JSON specific to Question, it's just oneOf and additionalProperties = false
interface Question {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant