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

v2: Unexpected error when loading sample OpenAPI document #2055

Closed
captainsafia opened this issue Jan 11, 2025 · 6 comments
Closed

v2: Unexpected error when loading sample OpenAPI document #2055

captainsafia opened this issue Jan 11, 2025 · 6 comments
Assignees
Milestone

Comments

@captainsafia
Copy link
Member

I am running into unexpected errors when loading an OpenAPI document from tests via the OpenApiDocument.Load APIs in 2.0.0-preview4.

Repro code:

using Microsoft.OpenApi;
using Microsoft.OpenApi.Models;
using System.IO;
using System.Text;

var contents = """
{
  "openapi": "3.1.1",
  "info": {
    "title": "GetDocumentSample | v1",
    "version": "1.0.0"
  },
  "paths": {
    "/hello/{name}": {
      "get": {
        "tags": [
          {
            "name": "GetDocumentSample"
          }
        ],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "GetDocumentSample"
    }
  ]
}
""";

var (Document, Diagnostic) = OpenApiDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(contents)), "json");
foreach (var error in Diagnostic.Errors)
{
	System.Console.WriteLine(error);
}

Produces the following error:

Expected a value.

There is no node associated with the error so it's not possible to determine what aspect of the document are incorrect. This document was serialized using the same version of the Microsoft.OpenApi package.

@MaggieKimani1
Copy link
Contributor

The issue stems from the tags value defined at the operation level.
Our parser expects to find an array of string but instead runs into a tag object which is invalid according to the spec:
Image

The root-level tags object however can be used to define multiple tag objects with additional metadata e.g: name, description etc.
Image

This should work:

var contents = """
{
  "openapi": "3.1.1",
  "info": {
    "title": "GetDocumentSample | v1",
    "version": "1.0.0"
  },
  "paths": {
    "/hello/{name}": {
      "get": {
        "tags": [
          "GetDocumentSample"
        ],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "GetDocumentSample"
    }
  ]
}
""";

@captainsafia
Copy link
Member Author

@MaggieKimani1 The OpenAPI document that I used in the example was serialized by the package itself so it seems like OpenAPI.NET is serializing operation-level tags in a way that it can be parsed by the package?

@MaggieKimani1
Copy link
Contributor

Could you please share the initial document you're working with before serialization for repro purposes?

@captainsafia
Copy link
Member Author

@MaggieKimani1 I updated the repro code to include the full round-tripping.

using Microsoft.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
using System.IO;
using System.Text;
using System.Collections.Generic;

var openApiDocument = new OpenApiDocument
{
    Info = new OpenApiInfo
    {
        Title = "GetDocumentSample | v1",
        Version = "1.0.0"
    },
    Paths = new OpenApiPaths
    {
        ["/hello/{name}"] = new OpenApiPathItem
        {
            Operations = new Dictionary<OperationType, OpenApiOperation>
            {
                [OperationType.Get] = new OpenApiOperation
                {
                    Tags = new List<OpenApiTag>
                            {
                                new OpenApiTag { Name = "GetDocumentSample" }
                            },
                    Parameters = new List<OpenApiParameter>
                            {
                                new OpenApiParameter
                                {
                                    Name = "name",
                                    In = ParameterLocation.Path,
                                    Required = true,
                                    Schema = new OpenApiSchema
                                    {
                                        Type = JsonSchemaType.String,
                                    }
                                }
                            },
                    Responses = new OpenApiResponses
                    {
                        ["200"] = new OpenApiResponse
                        {
                            Description = "OK",
                            Content = new Dictionary<string, OpenApiMediaType>
                            {
                                ["text/plain"] = new OpenApiMediaType
                                {
                                    Schema = new OpenApiSchema
                                    {
                                        Type = JsonSchemaType.String
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    Tags = new List<OpenApiTag>
            {
                new OpenApiTag { Name = "GetDocumentSample" }
            }
};

using var stream = new StringWriter();
var writer = new OpenApiJsonWriter(stream);
openApiDocument.SerializeAsV31(writer);
writer.Flush();

var (Document, Diagnostic) = OpenApiDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(stream.ToString())), "json");
foreach (var error in Diagnostic.Errors)
{
    System.Console.WriteLine(error);
}

@captainsafia
Copy link
Member Author

@MaggieKimani1 This appears to be resolved in 2.0.0-preview5 with all the OpenApiTagReference related changes.

@baywet
Copy link
Member

baywet commented Jan 28, 2025

Thanks for confirming @captainsafia ! closing

@baywet baywet closed this as completed Jan 28, 2025
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

3 participants