microsoft / microsoft/OpenAPI.NET

Regression: broken nullability after update to v3.9.0

Offen
#2,967 18 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

help wanted type:feature
Vorherrschende Sprache
C#
Sterne
1.6k
Forks
286
Ø Merge
6 Std. 38 Min.
Gemergte PRs (30 T.)
35

Beschreibung

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: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/4065

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die bereitgestellte C#-Reproduktion über SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0) auszuführen und die Ausgaben von v3.7.0 und v3.9.0 zu vergleichen. Verfolge die Serialisierung von OpenAPI 3.0-Schemas für JsonSchemaType.Null in additionalProperties und ein aus allOf abgeleitetes Schema; als erledigt gilt die Aufgabe, wenn nullable in beiden Fällen erhalten bleibt, ohne die gezeigte Ausgabe für andere Felder zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp
Bereich
api
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.