Skip to content

Copy constructor doesn't create new OpenApiPathItem objects when creating new instance of OpenApiPaths #1193

Description

Describe the bug
Copy constructors doesn't create a new instance of OpenApiPathItem and its constituent properties on new OpenApiDocument(document).

To Reproduce
I have created a breaking test to demonstrate this:

[Fact]
public void CopyConstructorForAdvancedDocumentWorks()
{
// Arrange & Act
var doc = new OpenApiDocument(AdvancedDocument);
// Change value of operation id for a given url
var docOpId = doc.Paths["/pets"].Operations[OperationType.Get].OperationId = "findAllMyPets";
var advancedDocOpId = AdvancedDocument.Paths["/pets"].Operations[OperationType.Get].OperationId;
// Assert
Assert.NotNull(doc.Info);
Assert.NotNull(doc.Servers);
Assert.NotNull(doc.Paths);
Assert.Equal(2, doc.Paths.Count);
Assert.NotNull(doc.Components);
Assert.NotEqual(docOpId, advancedDocOpId);
}

This is in the branch: is/copy-ctrs

Expected behavior
In the test, I change the operationId of the cloned OpenAPI document. I don't expect the operationId of the original OpenAPI document to also be updated.

Activity

  1. darrelmiller commented on Apr 2, 2023

    @darrelmiller
    Member

    When cloning OpenApiPaths we are using the standard Dictionary constructor which does not do a clone of the values of the key value pairs.
    https://github.com/microsoft/OpenAPI.NET/blob/vnext/src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs#L31

  2. MaggieKimani1 commented on Apr 24, 2023

    @MaggieKimani1
    Contributor

    Reopening this issue as its not fixed.

  3. kevinharing commented on Dec 1, 2023

    @kevinharing

    Having this issue with cloning OpenApiPathItem as well. As a workaround I serialize to json via the built-in functionality and use ReadFragment to deserialize it again.

  4. baywet commented on Jul 14, 2026

    @baywet
    Member

    This is not a bug, it's by design. Dictionary creates shallow copies, removing the derived constructor would be a breaking change at this point. And the rest of the cloning infrastructure is using offering shallow copies as well.

    I've amended the doc comments in #2947 to address that.

    And if we get an issue/popular request to implement deep copies across the object model, we should do that at a later stage.

    Let me know if you have any additional comments or questions.

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

Metadata

Metadata

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions