Consider the following specification:
{
"openapi": "3.0.0",
"info": { "title": "", "version": "0" },
"paths": {
"/questions": {
"get": {
"responses": {
"200": {
"description": "",
"content": {
"application/json;charset=utf-8": {
"schema": {
"type": "array",
"items": { "$ref": "#/components/schemas/Question" }
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Question": {
"oneOf": [
{ "$ref": "#/components/schemas/ReadingQuestion" },
{ "$ref": "#/components/schemas/MathQuestion" }
],
"discriminator": {
"propertyName": "subject",
"mapping": {
"reading": "#/components/schemas/ReadingQuestion",
"math": "#/components/schemas/MathQuestion"
}
}
},
"ReadingQuestion": {
"type": "object",
"properties": {
"subject": { "type": "string", "enum": [ "reading" ] },
"questionText": { "type": "string" }
},
"required": [ "subject", "questionText" ]
},
"MathQuestion": {
"type": "object",
"properties": {
"subject": { "type": "string", "enum": [ "math" ] },
"questionText": { "type": "string" },
"mathjax": { "type": "boolean" }
},
"required": [ "subject", "questionText", "mathjax" ]
}
}
}
}
Currently fromJSON @Question will parse
{ "subject": "math", "questionText": "What is 1 + 2?", "mathjax": false }
as
QuestionReadingQuestion ReadingQuestion{ readingQuestionQuestionText = "What is 1 + 2?" }
There are a number of issues and ways of fixing this (I can elaborate on other observations and ideas if desired) but I think the best/easiest change would be to make the fromJSON implementation respect the discriminator when it is present.
Consider the following specification:
{ "openapi": "3.0.0", "info": { "title": "", "version": "0" }, "paths": { "/questions": { "get": { "responses": { "200": { "description": "", "content": { "application/json;charset=utf-8": { "schema": { "type": "array", "items": { "$ref": "#/components/schemas/Question" } } } } } } } } }, "components": { "schemas": { "Question": { "oneOf": [ { "$ref": "#/components/schemas/ReadingQuestion" }, { "$ref": "#/components/schemas/MathQuestion" } ], "discriminator": { "propertyName": "subject", "mapping": { "reading": "#/components/schemas/ReadingQuestion", "math": "#/components/schemas/MathQuestion" } } }, "ReadingQuestion": { "type": "object", "properties": { "subject": { "type": "string", "enum": [ "reading" ] }, "questionText": { "type": "string" } }, "required": [ "subject", "questionText" ] }, "MathQuestion": { "type": "object", "properties": { "subject": { "type": "string", "enum": [ "math" ] }, "questionText": { "type": "string" }, "mathjax": { "type": "boolean" } }, "required": [ "subject", "questionText", "mathjax" ] } } } }Currently
fromJSON @Questionwill parse{ "subject": "math", "questionText": "What is 1 + 2?", "mathjax": false }as
There are a number of issues and ways of fixing this (I can elaborate on other observations and ideas if desired) but I think the best/easiest change would be to make the
fromJSONimplementation respect the discriminator when it is present.