Skip to content

Regression: broken nullability after update to v3.9.0 #2967

Description

Describe the bug
After updating from v3.7.0 to v3.9.0, the nullable property disappears when writing an OpenAPI 3.0 document.

Repro steps

using Microsoft.OpenApi;

var metaSchema = new OpenApiSchema
{
    Type = JsonSchemaType.Object,
    AdditionalProperties = new OpenApiSchema
    {
        Type = JsonSchemaType.Null
    }
};

var baseSchema = new OpenApiSchema
{
    Type = JsonSchemaType.Object,
    Id = "base",
    Properties = new Dictionary<string, IOpenApiSchema>
    {
        ["id"] = new OpenApiSchema
        {
            Type = JsonSchemaType.String
        }
    }
};

var derivedSchema = new OpenApiSchema
{
    Type = JsonSchemaType.Null,
    AllOf = new List<IOpenApiSchema>
    {
        new OpenApiSchemaReference(baseSchema.Id)
    },
    Properties = new Dictionary<string, IOpenApiSchema>
    {
        ["kind"] = new OpenApiSchema
        {
            Type = JsonSchemaType.String
        }
    }
};

var doc = new OpenApiDocument();
doc.Components ??= new OpenApiComponents();
doc.Components.Schemas ??= new Dictionary<string, IOpenApiSchema>();
doc.Components.Schemas.Add("meta", metaSchema);
doc.Components.Schemas.Add("base", baseSchema);
doc.Components.Schemas.Add("derived", derivedSchema);

var result = await doc.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);
Console.WriteLine(result);

Using v3.7.0, this prints:

{
  "openapi": "3.0.4",
  "info": { },
  "paths": { },
  "components": {
    "schemas": {
      "meta": {
        "type": "object",
        "additionalProperties": {
          "nullable": true
        }
      },
      "base": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          }
        }
      },
      "derived": {
        "allOf": [
          {
            "$ref": "#/components/schemas/base"
          }
        ],
        "properties": {
          "kind": {
            "type": "string"
          }
        },
        "nullable": true
      }
    }
  }
}

Using v3.9.0, this prints:

{
  "openapi": "3.0.4",
  "info": { },
  "paths": { },
  "components": {
    "schemas": {
      "meta": {
        "type": "object",
        "additionalProperties": {
          "enum": [
            null
          ]
        }
      },
      "base": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          }
        }
      },
      "derived": {
        "allOf": [
          {
            "$ref": "#/components/schemas/base"
          }
        ],
        "properties": {
          "kind": {
            "type": "string"
          }
        }
      }
    }
  }
}

Screenshots/Code Snippets
View of the diff:

Image

Originally reported at: domaindrivendev/Swashbuckle.AspNetCore#4065

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions