Haskell-OpenAPI-Client-Code-Generator icon indicating copy to clipboard operation
Haskell-OpenAPI-Client-Code-Generator copied to clipboard

Discriminator is not considered

Open chris-martin opened this issue 2 months ago • 0 comments

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.

chris-martin avatar Nov 17 '25 20:11 chris-martin