OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.43k stars 6.48k forks source link

[BUG][CSHARP-NETCORE] OneOf generator for not nullable primitives generates invalid code. #11426

Open jafin opened 2 years ago

jafin commented 2 years ago

Bug Report Checklist

Description

A template that contains oneOf logic for a not nullable primitives generates invalid code.

openapi-generator version

5.4 and ff7c2bdb591dd8fb6dd1c39dbe88367dbea4042d

OpenAPI declaration file content or url

Part of schema relating to this bug report:

PolymorphicProperty:
  oneOf:
  - type: boolean
  - type: string
  - type: object
  - type: array
     items:
  $ref: '#/components/schemas/StringArrayItem'
StringArrayItem:
  type: string
  format: string            

Full schema: https://gist.github.com/jafin/5b06f1a09e8a8c983bf25d61c47c1f4e

Generation Details

Checking non nullable primitive for null.

existing (compile error)

public PolymorphicProperty(bool actualInstance)
{
    //...
    this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
}

for primitives should not have the conditional check, i.e.

public PolymorphicProperty(bool actualInstance)
{
    //...
    this.ActualInstance = actualInstance;
}

XML Comments should be escaped to accomodate to Generic <> Types

existing

/// <summary>
/// Initializes a new instance of the <see cref="PolymorphicProperty" /> class
/// with the <see cref="List<string>" /> class
/// </summary>
/// <param name="actualInstance">An instance of List<string>.</param>

better

/// <summary>
/// Initializes a new instance of the <see cref="PolymorphicProperty" /> class
/// with the <see cref="List&lt;string&gt;" /> class
/// </summary>
/// <param name="actualInstance">An instance of List&lt;string&gt;.</param>

Getter for Generic Type has an invalid method name

existing (won't compile)

public List<string> GetList<string>()

should maybe be:

public List<string> GetListString()
Steps to reproduce

Generate csharp code:

java -jar openapi-generator-cli.jar generate -i firefly-iii-1.5.5.yaml -g csharp-netcore --library httpclient
Related issues/PRs
Suggest a fix
prajon84 commented 2 years ago

@jafin : Your update do not work if format: string is not defined

for e.g. below case:

type: array
  items:
    type: string 
0x326 commented 5 months ago

I can confirm the issue @jafin is running into. It seems to be caused by https://github.com/OpenAPITools/openapi-generator/blob/807250a4300f3a4fed65f990e7055b34047222e1/modules/openapi-generator/src/main/resources/csharp/modelOneOf.mustache#L38

I believe we can fix this by removing the code within the isPrimitiveType conditional block.

-this.ActualInstance = actualInstance{{^model.isNullable}}{{^isPrimitiveType}} ?? throw new ArgumentException("Invalid instance found. Must not be null."){{/isPrimitiveType}}{{#isPrimitiveType}}{{#isArray}} ?? throw new ArgumentException("Invalid instance found. Must not be null."){{/isArray}}{{/isPrimitiveType}}{{#isPrimitiveType}}{{#isFreeFormObject}} ?? throw new ArgumentException("Invalid instance found. Must not be null."){{/isFreeFormObject}}{{/isPrimitiveType}}{{#isPrimitiveType}}{{#isString}} ?? throw new ArgumentException("Invalid instance found. Must not be null."){{/isString}}{{/isPrimitiveType}}{{/model.isNullable}};
+this.ActualInstance = actualInstance{{^model.isNullable}}{{^isPrimitiveType}}{{/isPrimitiveType}}{{#isPrimitiveType}}{{#isArray}} ?? throw new ArgumentException("Invalid instance found. Must not be null."){{/isArray}}{{/isPrimitiveType}}{{#isPrimitiveType}}{{#isFreeFormObject}} ?? throw new ArgumentException("Invalid instance found. Must not be null."){{/isFreeFormObject}}{{/isPrimitiveType}}{{#isPrimitiveType}}{{#isString}} ?? throw new ArgumentException("Invalid instance found. Must not be null."){{/isString}}{{/isPrimitiveType}}{{/model.isNullable}};
wing328 commented 5 months ago

if anyone would like to contribute or sponsor a fix, please reply to let us know. thank you.

0x326 commented 5 months ago

@wing328 I would like to. I'll ping you when I have a PR.