Skip to content

Commit 737d180

Browse files
committed
Clone extensions in the schema copy constructor
1 parent 24ed6e3 commit 737d180

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ public OpenApiSchema(OpenApiSchema schema)
288288
ExternalDocs = schema?.ExternalDocs != null ? new(schema?.ExternalDocs) : null;
289289
Deprecated = schema?.Deprecated ?? Deprecated;
290290
Xml = schema?.Xml != null ? new(schema?.Xml) : null;
291+
Extensions = schema?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(schema.Extensions) : null;
291292
UnresolvedReference = schema?.UnresolvedReference ?? UnresolvedReference;
292293
Reference = schema?.Reference != null ? new(schema?.Reference) : null;
293294
}

test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using FluentAssertions;
1010
using Microsoft.OpenApi.Any;
1111
using Microsoft.OpenApi.Extensions;
12+
using Microsoft.OpenApi.Interfaces;
1213
using Microsoft.OpenApi.Models;
1314
using Microsoft.OpenApi.Writers;
1415
using VerifyXunit;
@@ -479,5 +480,29 @@ public void OpenApiSchemaCopyConstructorSucceeds()
479480
Assert.Equal("date", actualSchema.Format);
480481
Assert.True(actualSchema.Nullable);
481482
}
483+
484+
[Fact]
485+
public void CloningSchemaExtensionsWorks()
486+
{
487+
// Arrange
488+
var schema = new OpenApiSchema
489+
{
490+
Extensions =
491+
{
492+
{ "x-myextension", new OpenApiInteger(42) }
493+
}
494+
};
495+
496+
// Act && Assert
497+
var schemaCopy = new OpenApiSchema(schema);
498+
Assert.Equal(1, schemaCopy.Extensions.Count);
499+
500+
// Act && Assert
501+
schemaCopy.Extensions = new Dictionary<string, IOpenApiExtension>
502+
{
503+
{ "x-myextension" , new OpenApiInteger(40) }
504+
};
505+
Assert.NotEqual(schema.Extensions, schemaCopy.Extensions);
506+
}
482507
}
483508
}

0 commit comments

Comments
 (0)