Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
4 of 6 tasks
ben-schreiber opened this issue Dec 24, 2024 · 0 comments
Open
4 of 6 tasks

Comments

@ben-schreiber
Copy link

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]`

...
@ben-schreiber ben-schreiber changed the title [BUG] Generator not honoring nullable: true for required&inlined objects [BUG] Generator not honoring nullable: true for required+inlined objects Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant