OpenAPITools / OpenAPITools/openapi-generator

[BUG][TYPESCRIPT] Inconsistent handling of required property by $ref

Open
#19,813 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When generating the TypeScript models from the following OpenAPI spec, the type property of CriterionGenericConjunction is marked as optional (type?), even though it is defined as required in the referenced CriterionResidenceCriteriaHandmadeEnumResidenceCriteriaFieldEnum schema.

This causes a TypeScript error because the type property should be required, not optional.

Error :
image

openapi-generator version

"@openapitools/openapi-generator-cli": "^2.13.13",

OpenAPI declaration file content or url
"CriterionGenericConjunction": {
  "type": "object",
  "allOf": [
    {
      "$ref": "#/components/schemas/CriterionResidenceCriteriaHandmadeEnumResidenceCriteriaFieldEnum"
    },
    {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "AND",
            "CONTAINS",
            "ENDS_WITH",
            "EQUALS",
            "GREATER_THAN",
            "GREATER_THAN_OR_EQUALS",
            "IN",
            "IS_NOT_NULL",
            "IS_NULL",
            "LESS_THAN",
            "LESS_THAN_OR_EQUALS",
            "LIKES",
            "NOT",
            "NOT_EQUALS",
            "OR",
            "STARTS_WITH"
          ]
        },
        "criteria": {
          "type": "array",
          "items": {
            "$ref": "#/components/schemas/CriterionCriteriaHandmadeEnumCriteriaFieldEnum"
          }
        }
      }
    }
  ]
},
"CriterionResidenceCriteriaHandmadeEnumResidenceCriteriaFieldEnum": {
  "required": [
    "type"
  ],
  "type": "object",
  "properties": {
    "type": {
      "type": "string"
    }
  },
  "discriminator": {
    "propertyName": "type"
  }
}
Generation Details

openapi-generator-cli generate -i http://localhost:5000/api/v3/api-docs/Aic -g typescript-fetch -o ./src/api/generated --skip-validate-spec --additional-properties=supportsES6=true,typescriptThreePlus=true

Related issues/PRs

Not found

Suggest a fix

Ensure that the type property in CriterionGenericConjunction is generated as required (type: string) based on the $ref to CriterionResidenceCriteriaHandmadeEnumResidenceCriteriaFieldEnum, which defines type as required.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided OpenAPI declaration and the openapi-generator-cli typescript-fetch command. Inspect how the TypeScript generator handles required properties across allOf and $ref, then add or update a regression test if the relevant test area is found. Done means CriterionGenericConjunction generates type as required rather than type?.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.