OpenAPITools / OpenAPITools/openapi-generator

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

Open
#11,426 4 comments 2 reactions 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
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

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 running the documented openapi-generator-cli command with the linked Firefly schema or gist and inspect the generated csharp-netcore output. Check the OneOf constructor, XML comments, and generic-type getter; done means the generated C# compiles, primitive values are not null-checked, XML generics are escaped, and getter names are valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.