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][C#] anyOf generates non-compilable code (null-coalescing operator on enum and guid) #18178

Open
5 of 6 tasks
PetervdHemel opened this issue Mar 20, 2024 · 2 comments
Open
5 of 6 tasks

Comments

@PetervdHemel
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

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

#17986
#5442
#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)

@frekvent-szabolcs
Copy link

frekvent-szabolcs commented Apr 19, 2024

Same issue with 7.4.0 and with this specification (Directus default spec):
oas.json

@andyatshippo
Copy link

Same issue with 7.8.0 and this specification (Shippo public API)

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

3 participants