OpenAPITools / OpenAPITools/openapi-generator
[BUG] ELM: C# 8 nullable feature creates uncompilable (Maybe OneOf<PContactInfo>)
Nobody has claimed this yet.
- 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?
No, it is the complete swagger.json - Have you validated the input using an OpenAPI validator (example)?
Yes - What's the version of OpenAPI Generator used?
4.2.0 - Have you search for related issues/PRs?
Yes. - What's the actual output vs expected output?
- [Optional] Bounty to sponsor the fix (example)
Description
As of C# 8, nullable references types have an anotation '?', and unless it is there, the reference cannot be null.
https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/nullable-reference-types
I have a plain DTO like this,
public class AccResponse
{
// ReSharper disable once NotNullMemberIsNotInitialized
[NotNull] [Required] public BaseResponse Common { get; set; } = new BaseResponse();
// public string Id { get; set; }
[Required] public string Name { get; set; } = "";
public bool AllowGuest { get; set; }
public PContactInfo? TechContactInfo { get; set; }
public PContactInfo? AdminContactInfo { get; set; }
public bool Blocked { get; set; }
public bool Disabled { get; set; }
public int MaximumAllowedNamedUsers { get; set; }
public string? CreateUserEmailMessage { get; set; }
public string? CreateUserEmailMessageSubject { get; set; }
}
and the problem is
public PContactInfo? TechContactInfo { get; set; }
It is actually exactly the same as
public PContactInfo TechContactInfo { get; set; }
in C# 7 and earlier, i.e. since there is no [Required], that field can be null.
openapi-generator version
4.2.0
OpenAPI declaration file content or url
Command line used for generation
java -jar openapi-generator-cli-4.2.0.jar generate -g elm -i swagger.json -o src/Swagger/
Steps to reproduce
Just generate and look at the code. There is a warning for another problem,
[main] INFO o.o.codegen.DefaultGenerator - Model ExcelFormattingBase not generated since it's a free-form object
but you can ignore that. I can simplify those objects.
Suggest a fix
Just make it be treated as if neither ? or [Required] is not there.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the reported OpenAPI Generator 4.2.0 command with swagger.json or swagger.zip and inspect the generated DTOs, especially the PContactInfo properties. Compare nullable reference annotations with the issue's C# 7 and C# 8 examples. Done means generation no longer produces the reported uncompilable nullable output while preserving the stated Required behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, elm, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100