OpenAPITools / OpenAPITools/openapi-generator

[BUG][C#] anyOf generates non-compilable code (null-coalescing operator on enum and guid)

Open
#18,178 2 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 (example)
Description

Ever since updating to v7.4.0 which claims to 'fix anyof, oneof mixed primitive object parse error' I've been unable to generate, producing the following errors:

error CS0019: Operator '??' cannot be applied to operands of 
 type 'Guid' and '<throw expression>'

Which leads to this generated C# code:

public ParentId(Guid actualInstance)
{
        this.IsNullable = false;
        this.SchemaType= "anyOf";
        this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
}
error CS0019: Operator '??' cannot be applied to operands  
of type 'TaskStatusFilter' and '<throw expression>'

Where TaskStatusFilter is an enum, and leads to the following C# code:

public TaskStatus(TaskStatusFilter actualInstance)
{
        this.IsNullable = false;
        this.SchemaType= "anyOf";
        this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
}

Note that before v7.4.0 these exact issues did not exist, but I had issues with AnyOf instead, which is why I wanted to upgrade to solve this particular issue.

error CS0246: The type or namespace name 'AnyOf' could not be f 
ound (are you missing a using directive or an assembly reference?)

(related to a different part of the spec that uses 'AnyOf' including optional null value)

openapi-generator version

7.4.0

OpenAPI declaration file content or url

Example (shortened) openapi.json

Steps to reproduce

generate C# project with any specification file containing similar anyOf that are nullable

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/17986
https://github.com/OpenAPITools/openapi-generator/issues/5442
https://github.com/OpenAPITools/openapi-generator/issues/13384

Suggest a fix

The null-coalescing operator should not be applied to objects of type Guid or otherwise non-nullable types (eg custom types like TaskStatusFilter here)

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 with the C# anyOf generation path responsible for the constructors shown in the report, using the attached openapi.json as the reproduction input. Generate and compile the C# project; done means Guid and enum-backed constructors no longer emit invalid null-coalescing expressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.