Skip to content
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
19 changes: 11 additions & 8 deletions src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
if (DefaultMapping != null)
{
writer.WritePropertyName("defaultMapping");
DefaultMapping.SerializeAsV32(writer);
WriteMappingReference(writer, DefaultMapping);
}

// extensions
Expand All @@ -79,7 +79,7 @@
if (DefaultMapping != null)
{
writer.WritePropertyName("x-oas-default-mapping");
DefaultMapping.SerializeAsV31(writer);
WriteMappingReference(writer, DefaultMapping);
}

// extensions
Expand All @@ -98,7 +98,7 @@
writer.WriteEndObject();
}

private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version)

Check warning on line 101 in src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'version'.

Check warning on line 101 in src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'version'.

Check warning on line 101 in src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'version'.

Check warning on line 101 in src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'version'.
{
Utils.CheckArgumentNull(writer);

Expand All @@ -108,13 +108,16 @@
writer.WriteProperty(OpenApiConstants.PropertyName, PropertyName);

// mapping
writer.WriteOptionalMap(OpenApiConstants.Mapping, Mapping, (w, s) =>
writer.WriteOptionalMap(OpenApiConstants.Mapping, Mapping, WriteMappingReference);
}

private static void WriteMappingReference(IOpenApiWriter writer, OpenApiSchemaReference schemaReference)
{
var reference = schemaReference.Reference.ReferenceV3;
if (!string.IsNullOrEmpty(reference))
{
if (!string.IsNullOrEmpty(s.Reference.ReferenceV3) && s.Reference.ReferenceV3 is not null)
{
w.WriteValue(s.Reference.ReferenceV3);
}
});
writer.WriteValue(reference!);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.OpenApi.Tests.Models
{
[Collection("DefaultSettings")]
public class OpenApiDiscriminatorTests
{
[Theory]
[InlineData(OpenApiSpecVersion.OpenApi3_2, "defaultMapping", "Pet", true)]
[InlineData(OpenApiSpecVersion.OpenApi3_2, "defaultMapping", "#/components/schemas/Pet", true)]
[InlineData(OpenApiSpecVersion.OpenApi3_2, "defaultMapping", "Pet", false)]
[InlineData(OpenApiSpecVersion.OpenApi3_1, "x-oas-default-mapping", "Pet", true)]
[InlineData(OpenApiSpecVersion.OpenApi3_1, "x-oas-default-mapping", "#/components/schemas/Pet", true)]
[InlineData(OpenApiSpecVersion.OpenApi3_1, "x-oas-default-mapping", "Pet", false)]
public async Task SerializeDefaultMappingAsReferenceString(OpenApiSpecVersion specVersion, string propertyName, string referenceId, bool useDocument)
{
// Arrange
var document = useDocument ? new OpenApiDocument() : null;
var discriminator = new OpenApiDiscriminator
{
PropertyName = "pet_type",
DefaultMapping = new OpenApiSchemaReference(referenceId, document)
};

// Act
var actual = JsonNode.Parse(await discriminator.SerializeAsJsonAsync(specVersion));

// Assert
Assert.NotNull(actual);
Assert.Equal("#/components/schemas/Pet", actual[propertyName]?.GetValue<string>());
}

[Theory]
[InlineData(OpenApiSpecVersion.OpenApi3_0)]
[InlineData(OpenApiSpecVersion.OpenApi3_1)]
[InlineData(OpenApiSpecVersion.OpenApi3_2)]
public async Task SerializeMappingValuesAsReferenceStrings(OpenApiSpecVersion specVersion)
{
// Arrange
var document = new OpenApiDocument();
var discriminator = new OpenApiDiscriminator
{
PropertyName = "pet_type",
Mapping = new Dictionary<string, OpenApiSchemaReference>
{
["simple"] = new OpenApiSchemaReference("Pet", document),
["explicit"] = new OpenApiSchemaReference("#/components/schemas/Pet", document),
["noDocument"] = new OpenApiSchemaReference("Pet")
}
};

// Act
var actual = JsonNode.Parse(await discriminator.SerializeAsJsonAsync(specVersion));

// Assert
Assert.NotNull(actual);
var mapping = Assert.IsAssignableFrom<JsonObject>(actual["mapping"]);

Check warning on line 63 in test/Microsoft.OpenApi.Tests/Models/OpenApiDiscriminatorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The naming of Assert.IsAssignableFrom can be confusing. An overload of Assert.IsType is available with an exact match flag which can be set to false to perform the same operation.

See more on https://sonarcloud.io/project/issues?id=microsoft_OpenAPI.NET&issues=AZ9Bu30e22eGBhYIGG8S&open=AZ9Bu30e22eGBhYIGG8S&pullRequest=2931
AssertMappingValue(mapping, "simple");
AssertMappingValue(mapping, "explicit");
AssertMappingValue(mapping, "noDocument");
}

private static void AssertMappingValue(JsonObject mapping, string key)
{
var value = Assert.IsAssignableFrom<JsonValue>(mapping[key]);

Check warning on line 71 in test/Microsoft.OpenApi.Tests/Models/OpenApiDiscriminatorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The naming of Assert.IsAssignableFrom can be confusing. An overload of Assert.IsType is available with an exact match flag which can be set to false to perform the same operation.

See more on https://sonarcloud.io/project/issues?id=microsoft_OpenAPI.NET&issues=AZ9Bu30e22eGBhYIGG8R&open=AZ9Bu30e22eGBhYIGG8R&pullRequest=2931
Assert.Equal("#/components/schemas/Pet", value.GetValue<string>());
}
}
}
Loading