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

Nullable enum does not generate nullable property when using StringEnumConverter #2110

Closed
tomhreb opened this issue Apr 28, 2021 · 3 comments

Comments

@tomhreb
Copy link

tomhreb commented Apr 28, 2021

Hello,
I'm using Swashbuckle.AspNetCore.Annotations version 6.1.3 in netcoreapp3.1.
I have ConfigureServices as follows:

services.AddControllers()
  .AddNewtonsoftJson(options =>
  {
      serializerSettings.NullValueHandling = NullValueHandling.Ignore;
      options.SerializerSettings.Converters.Add(new StringEnumConverter());
  });

services.AddSwaggerGen(c => { .... })
  .AddSwaggerGenNewtonsoftSupport();

I have following Response class and ResponseType enum:

enum ResponseType{ A, B, C }

class Response
{
    public ResponseType? Type { get; set; }
}

In generated swagger.json, the Type property is not marked as nullable.

"Type": {
  "$ref": "#/components/schemas/ResponseType"
}

"ResponseType": {
    "enum": [ "A", "B", "C" ],
    "type": "string"
}
@Eneuman
Copy link
Contributor

Eneuman commented May 17, 2021

Try add the option "c.UseAllOfToExtendReferenceSchemas();" and see if that fixes it?

Refs cannot be marked as nullable so this might be a workaround.

@kipusoep
Copy link

UseAllOfToExtendReferenceSchemas did not work for me :-(

@martincostello
Copy link
Collaborator

To make issue tracking a bit less overwhelming for the new maintainers (see #2778), I've created a new tracking issue to roll-up various nullability issues here: #2793.

We'll refer back to this issue from there and include it as part of resolving that issue, but I'm going to close this one to help prune the backlog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants