Skip to content

OpenApi generate duplicate schemas #61408

@oPreis-Systecs

Description

@oPreis-Systecs

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In OpenApi (Version 9.0.4) schema json there are duplicate schemas if i use arrays.

Expected Behavior

Schemas should not duplicate.

Steps To Reproduce

Controller methods:

[HttpGet("new")]
[Produces("application/json")]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<EventDto>))]
public async Task<IActionResult> GetAllEventsWithImages()
{
    try
    {
        var events = await _eventLogic.GetAllNewEventsAsync();
        return Ok(events);
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Error while fetching new events.");
        return StatusCode((int)HttpStatusCode.InternalServerError, InternalServerErrorMessage);
    }
}

[HttpGet("{id:guid}")]
[Produces("application/json")]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(EventDto))]
public async Task<IActionResult> GetEventById(Guid id)
{
    _logger.LogInformation("Fetching event with ID {EventId}", id);
    var eventDto = await _eventLogic.GetEventByIdAsync(id);

    if (eventDto == null)
    {
        _logger.LogWarning("Event with ID {EventId} not found", id);
        return NotFound($"Event with ID {id} not found");
    }

    return Ok(eventDto);
}

Dto:

public class EventDto
{
    public Guid Id { get; set; }
    public Guid? CreatorId { get; set; }
    public string? InputText { get; set; }
    public string? OutputText { get; set; }
    public EventStatus? Status { get; set; }

    public IEnumerable<ImageDto> Images { get; set; } = new List<ImageDto>();
    public IEnumerable<CollectionDto> Collections { get; set; } = new List<CollectionDto>();
}

Generated schema:

{
  "openapi": "3.0.1",
  "info": {
    "title": "Systecs.PortfolioManager.Server | v1",
    "version": "1.0.0"
  },
  "paths": {
    "/api/Event/new": {
      "get": {
        "tags": [
          "Event"
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/EventDto"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/Event/{id}": {
      "get": {
        "tags": [
          "Event"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EventDto2"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "CollectionDto": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "surname": {
            "type": "string"
          },
          "description": {
            "type": "string",
            "nullable": true
          },
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "imageId": {
            "type": "string",
            "format": "uuid",
            "nullable": true
          },
          "image": {
            "$ref": "#/components/schemas/ImageDto2"
          },
          "events": {
            "type": "array",
            "items": { }
          }
        }
      },
      "CollectionDto2": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "surname": {
            "type": "string"
          },
          "description": {
            "type": "string",
            "nullable": true
          },
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "imageId": {
            "type": "string",
            "format": "uuid",
            "nullable": true
          },
          "image": {
            "$ref": "#/components/schemas/ImageDto2"
          },
          "events": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/EventDto"
            }
          }
        }
      },
      "EventDto": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "creatorId": {
            "type": "string",
            "format": "uuid",
            "nullable": true
          },
          "inputText": {
            "type": "string",
            "nullable": true
          },
          "outputText": {
            "type": "string",
            "nullable": true
          },
          "status": {
            "$ref": "#/components/schemas/NullableOfEventStatus"
          },
          "images": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ImageDto"
            }
          },
          "collections": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/CollectionDto"
            }
          }
        }
      },
      "EventDto2": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "creatorId": {
            "type": "string",
            "format": "uuid",
            "nullable": true
          },
          "inputText": {
            "type": "string",
            "nullable": true
          },
          "outputText": {
            "type": "string",
            "nullable": true
          },
          "status": {
            "$ref": "#/components/schemas/NullableOfEventStatus"
          },
          "images": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ImageDto"
            }
          },
          "collections": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/CollectionDto2"
            }
          }
        }
      },
      "ImageDto": {
        "required": [
          "imageData"
        ],
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "imageData": {
            "type": "string",
            "format": "byte"
          },
          "contentType": {
            "type": "string"
          },
          "type": {
            "$ref": "#/components/schemas/ImageType"
          }
        }
      },
      "ImageDto2": {
        "required": [
          "imageData"
        ],
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "imageData": {
            "type": "string",
            "format": "byte"
          },
          "contentType": {
            "type": "string"
          },
          "type": {
            "$ref": "#/components/schemas/ImageType"
          }
        },
        "nullable": true
      },
      "ImageType": {
        "type": "integer"
      },
      "NullableOfEventStatus": {
        "type": "integer",
        "nullable": true
      }
    }
  },
  "tags": [
    {
      "name": "Event"
    }
  ]
}

Exceptions (if any)

No response

.NET Version

9.0.200

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions