swagger-api / swagger-api/swagger-codegen

[C#][CSharp] Generating code for an array of enums produces incorrect output

Open
#5,329 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C-Sharp Feature: Enum help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

For models that contain array of items that are enums output enum code produced doesn't compile.

Swagger-codegen version

I've tried 2.2.1 and 2.2.2 - on both problem occurs.

Swagger declaration file content or url

Minimal example:

"TestModel": {
	"required": ["Login"],
	"type": "object",
	"properties": {
		"Login": {
			"maxLength": 255,
			"minLength": 0,
			"type": "string"
		},
		"Types": {
			"type": "array",
			"items": {
				"format": "int32",
				"enum": [0, 1, 2],
				"type": "integer"
			}
		}
	}
}

Full example json file:
https://gist.github.com/mkarpusiewicz/1c863c29ed08000bce05fbcf91bf1726

Command line used for generation

java -jar swagger-codegen-cli.jar generate -i enum-test.json -l csharp -o enum-test -DpackageName="WebApiTest"

Steps to reproduce
  1. With given enum.test.json use command line above to generate C# classes.
  2. TestModel.cs contains enum TypesEnum with members like:
[EnumMember(Value = ""0"")]
_0 = "0",

Int values are treated like strings and code doesn't compile.

Correct code should be:

[EnumMember(Value = "0")]
_0 = 0,
Related issues

I didn't find any related issue with arrays of enums.
Only one vaguely connected: https://github.com/swagger-api/swagger-codegen/issues/4670

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 enum-test.json and the generated TestModel.cs, using the documented swagger-codegen CLI command to reproduce the issue. Inspect how the array item's integer enum is rendered in the generated TypesEnum, then confirm the completed change by regenerating the model and compiling the C# output without the shown syntax or type errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.