Skip to content

OpenApiOperation copy constructor does not copy Responses property from source operation #995

Closed

Description

Describe the bug
When using the OpenApiOperation copy constructor to create a new instance from an existing instance, the new instance does not have the responses details from the existing instance's Responses property copied over.

This is preventing the end-to-end scenario we intended the copy-constructor to facilitate from working, i.e. in an ASP.NET Core minimal APIs project, the following API does not correctly have its schema described in Swagger:

using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI();

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", (int foo, [FromHeader]string bar) =>
{
    var forecast = Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi(o => new (o)
{
    Summary = "This is the summary",
    Description = "This is the description"
});

app.Run();

internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

To Reproduce
Steps to reproduce the current behavior:

  1. Use NuGet package Microsoft.OpenApi version 1.4.0.preview2
  2. In your program, create an instance of OpenApiOperation and add an OpenApiResponse instance to its Responses property
  3. Create a second instance of OpenApiOperation using the copy constructor and pass in the first instance
  4. Inspect the second instance

Expected behavior
The Responses property on the second instance of OpenApiOperation should contain instances of OpenApiResponse for each of the int response code keys and with suitable details from the first instance, at a minimum including the following properties:

  • Description
  • Content dictionary with an item per media type key with the following properties copied:
    • Example
    • Examples
    • Encoding

Screenshots/Code Snippets
Here's a console program that demonstrates the issue:

using System.Text.Json;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Quibble.CSharp;

var operation1 = new OpenApiOperation
{
    Description = "My API description",
    Summary = "My API summary",
    Tags = new List<OpenApiTag>
    {
        new() { Name = "Tag 1" },
        new() { Name = "Tag 2" }
    },
    Parameters = new List<OpenApiParameter>
    {
        new() { Name = "foo", In = ParameterLocation.Path, Required = true,
                Description = "The foo parameter", Example = new OpenApiString ("exampleValue") }
    },
    Responses = new()
    {
        {
            "200",
            new()
            {
                Description = "Success",
                Content = new Dictionary<string, OpenApiMediaType>
                {
                    { "application/json", new() },
                    { "text/plain", new() }
                }
            }
        },
        {
            "400",
            new()
            {
                Description = "Bad Request",
                Content = new Dictionary<string, OpenApiMediaType>
                {
                    { "application/problem+json", new() },
                    { "text/plain", new() }
                }
            }
        }
    }
};

var operation2 = new OpenApiOperation(operation1);

var operation1Json = JsonSerializer.Serialize(operation1);
var operation2Json = JsonSerializer.Serialize(operation2);

var diffs = JsonStrings.TextDiff(operation1Json, operation2Json);

foreach (var diff in diffs)
{
    Console.WriteLine(diff);
}

Console.ReadLine();

Outputs:

Type difference at $.RequestBody: null vs an object.
Object difference at $.Responses.
Left only properties: '200' (object), '400' (object).

Additional context
Another way of visualizing the issue.

First instance of OpenApiOperation JSON serialized:

{
    "Tags": [
        {
            "Name": "WebApplication8",
            "Description": null,
            "ExternalDocs": null,
            "Extensions": {},
            "UnresolvedReference": false,
            "Reference": null
        }
    ],
    "Summary": null,
    "Description": null,
    "ExternalDocs": null,
    "OperationId": "GetWeatherForecast",
    "Parameters": [],
    "RequestBody": null,
    "Responses": {
        "200": {
            "Description": "Success",
            "Headers": {},
            "Content": {
                "application/json": {
                    "Schema": null,
                    "Example": null,
                    "Examples": {},
                    "Encoding": {},
                    "Extensions": {}
                }
            },
            "Links": {},
            "Extensions": {},
            "UnresolvedReference": false,
            "Reference": null
        }
    },
    "Callbacks": {},
    "Deprecated": false,
    "Security": [],
    "Servers": [],
    "Extensions": {}
}

Second instance of OpenApiOperation JSON serialized:

{
    "Tags": [
        {
            "Name": "WebApplication8",
            "Description": null,
            "ExternalDocs": null,
            "Extensions": {},
            "UnresolvedReference": false,
            "Reference": null
        }
    ],
    "Summary": null,
    "Description": null,
    "ExternalDocs": null,
    "OperationId": "GetWeatherForecast",
    "Parameters": [],
    "RequestBody": {
        "UnresolvedReference": false,
        "Reference": null,
        "Description": null,
        "Required": false,
        "Content": null,
        "Extensions": null
    },
    "Responses": {},
    "Callbacks": {},
    "Deprecated": false,
    "Security": [],
    "Servers": [],
    "Extensions": {}
}

Activity

  1. added
    type:bugA broken experience
    priority:p1High priority but not blocking. Causes major but not critical loss of functionality SLA <=7days
    on Sep 26, 2022
  2. MaggieKimani1 commented on Sep 26, 2022

    @MaggieKimani1
    Contributor

    This is now fixed in v1.4.3

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

Metadata

Metadata

Labels

priority:p1High priority but not blocking. Causes major but not critical loss of functionality SLA <=7daystype:bugA broken experience

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions