OpenAPITools / OpenAPITools/openapi-generator

[BUG] Generator not honoring `nullable: true` for required+inlined objects

Open
#20,373 1 comment 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

The generator discards a nullable: true property for a required field which has an inlined type. This does not occur when the same property is a ref.

openapi-generator version

Installed via brew on macOS (15.1.1)

$ openapi-generator --version
openapi-generator-cli 7.10.0
  commit : 12dfe8f
  built  : -999999999-01-01T00:00:00+18:00
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: minimal-example
  description: ''
  license:
    name: ''
  version: 0.5.20
paths: {}
components:
  schemas:
    ObjectOne:
      type: object
      required:
      - one
      - two
      properties:
        one:
          allOf:
          - $ref: '#/components/schemas/OtherObject'
          nullable: true
        two:
          allOf:
          - type: string
          nullable: true
    OtherObject:
      type: string

Generation Details
Steps to reproduce
$ openapi-generator generate --input-spec "$HOME/schema.yaml" \
-g typescript-axios \
--additional-properties useSingleRequestParameter=true \
--additional-properties paramNaming=original \
--output "$HOME/Desktop/output/" \
-v

The resulting api.ts file is:

// Omitted for brevity 

/**
 * 
 * @export
 * @interface ObjectOne
 */
export interface ObjectOne {
    /**
     * 
     * @type {string}
     * @memberof ObjectOne
     */
    'one': string | null;
    /**
     * 
     * @type {string}
     * @memberof ObjectOne
     */
    'two': string;
}

Whereas, the desired output is

// Omitted for brevity 

/**
 * 
 * @export
 * @interface ObjectOne
 */
export interface ObjectOne {
    /**
     * 
     * @type {string}
     * @memberof ObjectOne
     */
    'one': string | null;
    /**
     * 
     * @type {string}
     * @memberof ObjectOne
     */
    'two': string | null; // <-- Change is here
}

Related issues/PRs
Suggest a fix

In the resulting debug logs, the modelJson is parsed and logged as follows (I removed \n and formatted for clarity's sake):

{
    "required": ["one", "two"],
    "type": "object",
    "properties": {
        "one": {
            "nullable": true,
            "allOf": [{ "$ref": "#/components/schemas/OtherObject" }]
        },
        "two": { "type": "string" }
    }
}

Notice how properties -> two does not contain "nullable": true even though the initial schema.yaml file does. This led me to believe that the is prevalent in all client generators. I confirmed by this by running the same generate command above with -g python. The output was:

...

class ObjectOne(BaseModel):
    """
    ObjectOne
    """ # noqa: E501
    one: Optional[StrictStr]
    two: StrictStr  # This should be `Optional[StrictStr]`

...

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 by reproducing the issue with the provided schema.yaml and the openapi-generator CLI using the typescript-axios generator, then compare the generated ObjectOne output with the Python generator result. Trace the logged modelJson for the inlined property and verify that the nullable setting is preserved; done means both generators emit the required two property as nullable.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.