Skip to content

Fix copy constructor for arrays and objects #1736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/Microsoft.OpenApi/Any/OpenApiAnyCloneHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Reflection;

namespace Microsoft.OpenApi.Any
{
/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.OpenApi/Any/OpenApiArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public OpenApiArray() { }
public OpenApiArray(OpenApiArray array)
{
AnyType = array.AnyType;
foreach (var item in array)
{
Add(OpenApiAnyCloneHelper.CloneFromCopyConstructor(item));
}
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.OpenApi/Any/OpenApiObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public OpenApiObject() { }
public OpenApiObject(OpenApiObject obj)
{
AnyType = obj.AnyType;
foreach (var key in obj.Keys)
{
this[key] = OpenApiAnyCloneHelper.CloneFromCopyConstructor(obj[key]);
}
}

/// <summary>
Expand Down
41 changes: 39 additions & 2 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.OpenApi.Any;
Expand Down Expand Up @@ -485,6 +483,45 @@ public void OpenApiSchemaCopyConstructorSucceeds()
Assert.True(actualSchema.Nullable);
}

public static TheoryData<IOpenApiAny> SchemaExamples()
{
return new()
{
new OpenApiArray() { new OpenApiString("example") },
new OpenApiBinary([0, 1, 2]),
new OpenApiBoolean(true),
new OpenApiByte(42),
new OpenApiDate(new(2024, 07, 19, 12, 34, 56)),
new OpenApiDateTime(new(2024, 07, 19, 12, 34, 56, new(01, 00, 00))),
new OpenApiDouble(42.37),
new OpenApiFloat(42.37f),
new OpenApiInteger(42),
new OpenApiLong(42),
new OpenApiNull(),
new OpenApiObject() { ["prop"] = new OpenApiString("example") },
new OpenApiPassword("secret"),
new OpenApiString("example"),
};
}

[Theory]
[MemberData(nameof(SchemaExamples))]
public void CloningSchemaExamplesWorks(IOpenApiAny example)
{
// Arrange
var schema = new OpenApiSchema
{
Example = example
};

// Act && Assert
var schemaCopy = new OpenApiSchema(schema);
Assert.NotNull(schemaCopy.Example);

// Act && Assert
Assert.Equivalent(schema.Example, schemaCopy.Example);
}

[Fact]
public void CloningSchemaExtensionsWorks()
{
Expand Down
Loading