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

Different StringEnumConverter-Behavior between STJ / Newtonsoft #2820

Closed
angelaki opened this issue Apr 22, 2024 · 5 comments · Fixed by #2825
Closed

Different StringEnumConverter-Behavior between STJ / Newtonsoft #2820

angelaki opened this issue Apr 22, 2024 · 5 comments · Fixed by #2825
Labels
help-wanted A change up for grabs for contributions from the community

Comments

@angelaki
Copy link
Contributor

Switching my project from Newtonsoft to STJ, I noticed the following different behavior between the two scheme generators:

Adding the JsonStringEnumConverter makes Swashbuckle aware of enums being serialized as string - so far, so good. But the STJ schema builder doesn't seam to be aware of IDictionary<[Enum], int>.

For Newtonsoft the following scheme was generated:

...
"properties": {
  "EnumValue1": {
	"type": "integer",
	"format": "int32"
  },
  "EnumValue2": {
	"type": "integer",
	"format": "int32"
  },
  "EnumValue3": {
	"type": "integer",
	"format": "int32"
  }
},
"additionalProperties": false,
...

Now I only get

"additionalProperties": {
  "type": "integer",
  "format": "int32"
},

So it's still being recognized as an object containing integer properties. Better than nothing. But the old scheme was way more explicit.

@martincostello
Copy link
Collaborator

This might be related to dotnet/runtime#47765 and this comment:

if (IsSupportedDictionary(type, out Type keyType, out Type valueType))
{
return DataContract.ForDictionary(
underlyingType: type,
valueType: valueType,
keys: null, // STJ doesn't currently support dictionaries with enum key types
jsonConverter: JsonConverterFunc);
}

It's possible this is fixed by #2799, otherwise we might have to see if there's any changes needed to if the assertion in that comment is no longer true.

@martincostello
Copy link
Collaborator

You may have to opt-in to this behaviour with JsonSerializerOptions.DictionaryKeyPolicy.

@angelaki
Copy link
Contributor Author

Thank you so much for checking! Butt sorry, not quite sure what you mean? I've set options.JsonSerializerOptions.DictionaryKeyPolicy = System.Text.Json.JsonNamingPolicy.CamelCase; for testing purposes now but no difference on the output.

The serializer itself works (using .Net 8.0, but also checked 7 & 6), generating strings for the keys.

@martincostello martincostello added the help-wanted A change up for grabs for contributions from the community label Apr 22, 2024
@angelaki
Copy link
Contributor Author

I could sure support here with a PR :) Can you give me a hint where to start?

@martincostello
Copy link
Collaborator

I'm not sure where specifically without starting to debug it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help-wanted A change up for grabs for contributions from the community
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants