question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default value not working for enum's

See original GitHub issue

Default value is not working for enums, e.g.:

        public ActionResult<PagedResults<string>> Get(
            [FromQuery, BindRequired] string q,
            [FromQuery]int page = 1,
            [FromQuery]int pageSize = 20,
            [FromQuery]AutocompleteMatchType matchType = AutocompleteMatchType.BeginFirst)

then in the generated JSON:

"parameters": [
  {
	"name": "q",
	"in": "query",
	"required": true,
	"schema": {
	  "type": "string"
	}
  },
  {
	"name": "page",
	"in": "query",
	"schema": {
	  "type": "integer",
	  "format": "int32",
	  "default": 1
	}
  },
  {
	"name": "pageSize",
	"in": "query",
	"schema": {
	  "type": "integer",
	  "format": "int32",
	  "default": 20
	}
  },
  {
	"name": "matchType",
	"in": "query",
	"schema": {
	  "enum": [
		"begin",
		"any",
		"beginFirst"
	  ],
	  "type": "string"
	}
  }
]

Noting that there is no default value in the JSON for the enum, even though one exists.

This seems to be down to the fact that OpenApiPrimitiveFactory.FactoryMethodMap does not contain any way to create an OpenApiPrimitive<T> for an enum, therefore the call to schema.Default = OpenApiPrimitiveFactory.CreateFrom(parameterInfo.DefaultValue); returns and sets a null value within the method SwaggerGenerator.GenerateParameter(...) making it look from then on as if there was no default value (even though parameterInfo.DefaultValue with the correct default value does exist, and is passed in to that call).

I’d be interested to work on this if it’s considered useful. I’m not quite sure if it is even possible to generate an OpenApiPrimitive<T> where T is an enum type? If so, then some reflection-based code added in to OpenApiPrimitiveFactory.CreateFrom(...) to deal with that sounds like it might be the way to go? (Noting that the default value for an enum depends on whether the values for the enum are being output as string or int, so it would be important to display the default value in the correct format as well.)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
PawelGerrcommented, Jul 10, 2020

@michaelakin My workaround is to use UseInlineDefinitionsForEnums

services.AddSwaggerGen(options =>
{
    options.UseInlineDefinitionsForEnums();
3reactions
Zoidborg7commented, Jul 10, 2020

@michaelakin My workaround is to use UseInlineDefinitionsForEnums

services.AddSwaggerGen(options =>
{
    options.UseInlineDefinitionsForEnums();

If I use this method even if I have StringEnumConverter added to the Json serializer configuration, my API params now show just the numbers for the enum types instead of the names. The names have meaning to the API consumer, but the number values alone do not. I really need a default to be specified but with the enum being represented by its names not the number values.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the default value for enum variable?
The default value of an enum E is the value produced by the expression (E)0 . As an example, take the following enum:...
Read more >
.Net Tips — marking the default enum value 'Undefined ...
The problem with default value is that enum is really an int underneath, and as do all integers in C# an enum has...
Read more >
Enumeration types - C# reference
The default value of an enumeration type E is the value produced by expression (E)0 , even if zero doesn't have the corresponding...
Read more >
Enum as field/property not initialized with default value
Hi! I found a problem with how Enums are being handled when compiling to Java. On C# (language level) Enums are value type....
Read more >
Assigning a default value if one doesnt exist in enum
I wanted to know if it was possible to set a default value for a enum class if a value doesn't exist in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found