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.

Nullable strings are not marked nullable in openapi json

See original GitHub issue

When I use a nullable property, they get marked as "nullable": true in the json output, which is correct.

But it seems this is not the case for strings. Is this a bug or is it me just being really thick here?

I tried adding [JsonProperty(Required = Required.AllowNull)] to the property, but that didn’t work either.

This is with version 0.7.2

This is my model

    public class Subscription
    {
        public string TheString { get; set; }
        public string? TheNullableString { get; set; }

        public int TheInt { get; set; }
        public int? TheNullableInt { get; set; }

        public DateTimeOffset TheDateTime { get; set; }
        public DateTimeOffset? TheNullableDateTime { get; set; }

And this is the output from http://localhost:7071/api/openapi/v3.json

      "subscription": {
        "type": "object",
        "properties": {
          "theString": {
            "type": "string"
          },
          "theNullableString": {
            "type": "string"    <--- missing nullable here!
          },
          "theInt": {
            "type": "integer",
            "format": "int32"
          },
          "theNullableInt": {
            "type": "integer",
            "format": "int32",
            "nullable": true
          },
          "theDateTime": {
            "type": "string",
            "format": "date-time"
          },
          "theNullableDateTime": {
            "type": "string",
            "format": "date-time",
            "nullable": true
          },

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
MartinWickmancommented, Jun 22, 2021

Just to make it clear though:

If you have nullable references, c# 8 enabled in your project (as I have) then string email is not allowed to be null. You get a compilation error or warning if you try to assign it to null. So you absolutely should to put a ? on all nullable reference types.

So imo, it’s reasonable to expect string? to generate nullable: true in the swagger.json output. But as it stands now, you must remember to add the [OpenApiProperty(Nullable=true) for reference types. I don’t really know what gets generated in a default “non-nullable” project though.

So if you craft the REST client yourself, this is easy enough to handle, but problem is when you generate a client (in my case with Nswag), it will generate the C#-properties in the model classes without the proper “allow null” attributes, thus crashing when the server sends a null json value for that property. You end up with Newtonsoft.Json: Required property 'email' expects a non-null value.

Actually, it generates them like this:

[Newtonsoft.Json.JsonProperty("email", Required = Newtonsoft.Json.Required.DisallowNull)]
public string Email { get; set; }

Which is due to the property missing nullable: true in the swagger spec.

Otherwise, I agree with your logic: [JsonRequired] overrides [OpenApiProperty(Nullable)] which overrides the property. And if the property is nullable (?) it should generate nullable: true imo.

1reaction
justinyoocommented, Jun 21, 2021

@MartinWickman I’ve updated the OpenApiPropertyAttribute that you can make use of for your nullable properties. It will be released in the next release. In the meantime, it would be great if you try the code on your end.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mark strings as non-nullable in ASP.NET Core 3.0 Swagger
This Swashbuckle issue shows that currently optional strings don't have the nullable:true attribute and end up non-nullable. The fix will be ...
Read more >
Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >
x-nullable
Use x-nullable in your OpenAPI 2.0 documents to mark schemas with the label Nullable in the API documentation. This indicates that the value...
Read more >
Required and nullable elements
The image shows the required and nullable fields selection from the imported Swagger JSON file. When you use this process object in a...
Read more >
OpenAPI validation failed for nullable fields
OpenAPI assertion uses json schema validator (v4) to validate the body. It seemed that nullable field is applicable to Open API 3.0 spec ......
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