openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open ben-schreiber opened this issue 1 year ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [ ] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] 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]`

...

ben-schreiber avatar Dec 24 '24 14:12 ben-schreiber

I have a similar issue, but the behavior is not affected by the required field. I'm using Using 7.14.0-SNAPSHOT via Docker. My openapi.yaml:

  date_estimate_to_iso8601:
    nullable: true
    type: string
    title: Date estimate to, for planning - null or not set are equal

The field is not required, but other fields that are required experience the same problem. But in my case the modelJson generated by openapi-generator is always correct for all the fields:

    "date_estimate_to_iso8601" : {
      "nullable" : true,
      "title" : "Date estimate to, for planning - null or not set are equal",
      "type" : "string"
    },

The typescript-jquery parser output:


    date_estimate_to_iso8601?: string;

Expected:


    date_estimate_to_iso8601?: string | null;

To be noted that with the same openapi.yaml, the generated code for Rust-axum is correct with Option<Nullable<T>>:

    #[serde(rename = "date_estimate_to_iso8601")]
    #[serde(deserialize_with = "deserialize_optional_nullable")]
    #[serde(default = "default_optional_nullable")]
    #[serde(skip_serializing_if="Option::is_none")]
    pub date_estimate_to_iso8601: Option<Nullable<String>>,

dgasparri avatar Jun 06 '25 12:06 dgasparri