Skip to content

SerializeAsV2 produces invalid swagger (when SerializeAsV3 is valid)Β #425

@pechkarus

Description

@pechkarus

When creating Open API document and specifying schema reference for enum in query parameter (as oppose to define schema in place) invalid Swagger is generated for OpenAPI/Swagger v2, on the other hand v3 document is perfectly valid.
Correct me if I am wrong, but schema of parameters in v2 should be defined in place and cannot be referenced. However it adds additional burden on the authors of Open API documents who want to support V2 and V3 simultaneously. Should the generator be smart enough to follow the reference and inline the schema definition when generated for v2, while supporting v3 features at the same time?
Related issue in Swashbuckle.AspNetCore

Version

Microsoft.OpenApi 1.1.4.0

Program

Slightly modified default example to show the issue:

[JsonConverter(typeof(StringEnumConverter))]
public enum JustAnotherEnum
{
    A = 0,
    B = 1,
    C = 2,
    D = 10,
    E = 11,
}
var json = new OpenApiDocument
{
    Info = new OpenApiInfo
    {
        Version = "1.0.0",
        Title = "Just a Swagger ",
    },
    Servers = new List<OpenApiServer>
    {
        new OpenApiServer { Url = "http://just.swagger.io/api" }
    },
    Components = new OpenApiComponents
    {
        Schemas =
        {
            ["JustAnotherEnum"] = new OpenApiSchema
            {
                Type = "string",
                Enum = typeof(JustAnotherEnum)
                        .GetEnumValues()
                        .Cast<object>()
                        .Select(value => (IOpenApiAny)new OpenApiString(value.ToString()))
                        .ToList()
            },            
        }
    },
    Paths = new OpenApiPaths
    {
        ["/justCall"] = new OpenApiPathItem
        {
            Operations = new Dictionary<OperationType, OpenApiOperation>
            {
                [OperationType.Get] = new OpenApiOperation
                {
                    Description = "Just returns",
                    Parameters =
                    {
                        new OpenApiParameter()
                        {
                            In = ParameterLocation.Query,
                            Name = "justString",
                            Schema = new OpenApiSchema
                            {
                                Type = "string",                                            
                            }
                       },
                       new OpenApiParameter()
                       {
                           In = ParameterLocation.Query,
                           Name = "justEnum",
                           Schema = new OpenApiSchema
                           {
                               Reference = new OpenApiReference()
                               {
                                   Id = "JustAnotherEnum",
                                   Type = ReferenceType.Schema
                               }
                           }  
                        }
                    },  
                    Responses = new OpenApiResponses
                    {
                        ["200"] = new OpenApiResponse
                        {
                            Description = "OK"
                        }
                    }
                }
            }
        }
    }
}.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json);

Expected

Valid OpenAPI/Swagger v2 document

Actual

Invalid OpenAPI/Swagger v2 document

{
  "swagger": "2.0",
  "info": {
    "title": "Just a Swagger ",
    "version": "1.0.0"
  },
  "host": "just.swagger.io",
  "basePath": "/api",
  "schemes": [
    "http"
  ],
  "paths": {
    "/justCall": {
      "get": {
        "description": "Just returns",
        "parameters": [
          {
            "in": "query",
            "name": "justString",
            "type": "string"
          },
          {
            "in": "query",
            "name": "justEnum"
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  },
  "definitions": {
    "JustAnotherEnum": {
      "enum": [
        "A",
        "B",
        "C",
        "D",
        "E"
      ],
      "type": "string"
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions