OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-axios] The `nullSafeAdditionalProps` option should also work on objects with specified keys.

Open
#17,045 0 comments 1 reaction 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

In the object schema, nullSafeAdditionalProps is applied correctly when additionalProperties is set as a single property. However, if there are predefined keys and additionalProperties is defined, it generates them as required in their type.

this makes type error

  • test
image
  • test-2
image
openapi-generator version

7.0.1

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  title: "title"
  version: "1.0.0"
paths:
  /test:
    get:
      responses:
        "200":
          description: "200 OK"
          content:
            application/json:
              schema:
                type: "object"
                properties:
                  data:
                    type: "object"
                    properties:
                      a:
                        type: "boolean"
                      b:
                        type: "boolean"
                    additionalProperties:
                       type: "boolean"
  /test-2:
    get:
      responses:
        "200":
          description: "200 OK"
          content:
            application/json:
              schema:
                    additionalProperties:
                       type: "boolean"
Generation Details
{
    "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
    "spaces": 2,
    "generator-cli": {
        "version": "7.0.1",
        "generators": {
            "typescript-axios": {
                "glob": "api-specs/*.yaml",
                "output": "#{cwd}/src/service/#{name}",
                "generatorName": "typescript-axios",
                "additionalProperties": {
                    "useSingleRequestParameter": true,
                    "nullSafeAdditionalProps": true
                }
            }
        }
    }
}
Steps to reproduce

use embeded yaml to codegen

Related issues/PRs
Suggest a fix

Shouldn't the properties be declared as optional with '?' in the key declaration, even without nullSafeAdditionalProps?

https://github.com/OpenAPITools/openapi-generator/blob/2f655f1a9cec1f69bcd3487ea67d0d137dd7ac8d/modules/openapi-generator/src/main/resources/typescript-axios/modelGeneric.mustache#L8

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

Start with the embedded OpenAPI YAML and generator-cli configuration, then inspect modules/openapi-generator/src/main/resources/typescript-axios/modelGeneric.mustache at the linked line. Generate the TypeScript Axios models for both object schemas and compare the property declarations. Done means nullSafeAdditionalProps also produces usable types for objects with predefined keys without the reported type error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.