Skip to content

Commit

Permalink
Merge pull request #1270 from microsoft/mk/fix-schema-copy-ctr
Browse files Browse the repository at this point in the history
Clone extensions in the OpenApiSchema copy constructor
  • Loading branch information
MaggieKimani1 authored Jun 15, 2023
2 parents 24ed6e3 + 7d0df91 commit ca33d2b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public OpenApiSchema(OpenApiSchema schema)
ExternalDocs = schema?.ExternalDocs != null ? new(schema?.ExternalDocs) : null;
Deprecated = schema?.Deprecated ?? Deprecated;
Xml = schema?.Xml != null ? new(schema?.Xml) : null;
Extensions = schema?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(schema.Extensions) : null;
UnresolvedReference = schema?.UnresolvedReference ?? UnresolvedReference;
Reference = schema?.Reference != null ? new(schema?.Reference) : null;
}
Expand Down
25 changes: 25 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
using VerifyXunit;
Expand Down Expand Up @@ -479,5 +480,29 @@ public void OpenApiSchemaCopyConstructorSucceeds()
Assert.Equal("date", actualSchema.Format);
Assert.True(actualSchema.Nullable);
}

[Fact]
public void CloningSchemaExtensionsWorks()
{
// Arrange
var schema = new OpenApiSchema
{
Extensions =
{
{ "x-myextension", new OpenApiInteger(42) }
}
};

// Act && Assert
var schemaCopy = new OpenApiSchema(schema);
Assert.Equal(1, schemaCopy.Extensions.Count);

// Act && Assert
schemaCopy.Extensions = new Dictionary<string, IOpenApiExtension>
{
{ "x-myextension" , new OpenApiInteger(40) }
};
Assert.NotEqual(schema.Extensions, schemaCopy.Extensions);
}
}
}
2 changes: 2 additions & 0 deletions test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,8 @@ namespace Microsoft.OpenApi.Models
Callback = 8,
[Microsoft.OpenApi.Attributes.Display("tags")]
Tag = 9,
[Microsoft.OpenApi.Attributes.Display("paths")]
Path = 10,
}
public class RuntimeExpressionAnyWrapper : Microsoft.OpenApi.Interfaces.IOpenApiElement
{
Expand Down

0 comments on commit ca33d2b

Please sign in to comment.