OpenAPITools / OpenAPITools/openapi-generator

[BUG][csharp] Error with using enum and default

Open
#15,681 0 comments 0 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). Happy to chip in $100 if this is actually an issue!
Description

I have a model with a property. The property is an enum with a default. If I try to instantiate the model I get an argument error (see below). If I change the constructor on the generated model so the default for the argument is null then the error is resolved.

Here's the relevant input:
https://github.com/jason-o-matic/docraptor-csharp/blob/852a3895792292b4483eb43d17be07fc02556503/docraptor.yaml#L391-L398

Here's the code that produces the error:
https://github.com/jason-o-matic/docraptor-csharp/blob/852a3895792292b4483eb43d17be07fc02556503/test/prince_options.cs#L43-L45

Here's the error message I get when running that code:

test/prince_options.cs(43,22): error CS1729: The type `DocRaptor.Model.PrinceOptions' does not contain a constructor that takes `24' arguments
/app/bin/DocRaptorWithDependencies.dll (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings

Here's the commit where you can see the change that fixes the issue:
https://github.com/jason-o-matic/docraptor-csharp/commit/0a3f87bd2a52afa2e002da8c1b0284cd948a555e

That line is pretty long, but the important part is that

InputEnum? input = InputEnum.Html

changes to

InputEnum? input = null

I'm not super familiar C# so I'm not really sure why this is erroring in the first place. It seems like using an enum as an argument default should be fine. It seems like switching to null is reasonable because there's https://github.com/jason-o-matic/docraptor-csharp/blob/852a3895792292b4483eb43d17be07fc02556503/src/DocRaptor/Model/PrinceOptions.cs#L134-L141 in the constructor to set the default.

openapi-generator version

I got the same error on 6.6.0 and several previous versions.

OpenAPI declaration file content or url

https://raw.githubusercontent.com/jason-o-matic/docraptor-csharp/852a3895792292b4483eb43d17be07fc02556503/docraptor.yaml

Generation Details

This was used in the above repo to generate the client:

docker run -it -w /local --rm -v /Users/jason/projects/docraptor/docraptor-csharp:/local openapitools/openapi-generator-cli:v6.6.0 generate -i docraptor.yaml -g csharp -c generator-config.json

Running script/test in the above repo produced the error. It basically uses several different docker mono versions to run the files in the test directory.

Steps to reproduce
  1. Generate the client where a model has an enum parameter with a default
  2. Run code to instantiate that model
Related issues/PRs

I looked through https://github.com/OpenAPITools/openapi-generator/issues?q=is%3Aissue+is%3Aopen+enum+default+csharp but didn't see anything I recognized as being the same.

Suggest a fix

It seems that switching the default value of the argument to null fixes the issue, but I'm not sure if that's the best solution. I noticed the other arguments used default() around the default argument, so maybe that's another option, but perhaps using the enum inside default() would still cause an issue?

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 linked PrinceOptions.cs and the provided OpenAPI declaration, then reproduce using the documented openapi-generator CLI command and the C# generator. Compare the generated enum parameter default with the constructor's default-setting logic and run the generated project's test command. Done means the generated client compiles and model instantiation succeeds with an enum default.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.