Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 10 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
/// Initializes a copy of <see cref="IOpenApiSchema"/> object
/// </summary>
/// <param name="schema">The schema object to copy from.</param>
internal OpenApiSchema(IOpenApiSchema schema)

Check warning on line 267 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this constructor to reduce its Cognitive Complexity from 20 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
Utils.CheckArgumentNull(schema);
Title = schema.Title ?? Title;
Expand Down Expand Up @@ -336,23 +336,23 @@
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
}

private static void SerializeBounds(IOpenApiWriter writer, OpenApiSpecVersion version, string propertyName, string exclusivePropertyName, string isExclusivePropertyName, string? value, string? exclusiveValue, bool? isExclusiveValue)

Check warning on line 339 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Method has 8 parameters, which is greater than the 7 authorized. (https://rules.sonarsource.com/csharp/RSPEC-107)

Check warning on line 339 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
if (version >= OpenApiSpecVersion.OpenApi3_1)
{
if (!string.IsNullOrEmpty(exclusiveValue) && exclusiveValue is not null)

Check warning on line 343 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// was explicitly set in the document or object model
writer.WritePropertyName(exclusivePropertyName);
writer.WriteRaw(exclusiveValue);
}
else if (isExclusiveValue == true && !string.IsNullOrEmpty(value) && value is not null)

Check warning on line 349 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// came from parsing an old document
writer.WritePropertyName(exclusivePropertyName);
writer.WriteRaw(value);
}
else if (!string.IsNullOrEmpty(value) && value is not null)

Check warning on line 355 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// was explicitly set in the document or object model
writer.WritePropertyName(propertyName);
Expand All @@ -361,14 +361,14 @@
}
else
{
if (!string.IsNullOrEmpty(exclusiveValue) && exclusiveValue is not null)

Check warning on line 364 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// was explicitly set in a new document being downcast or object model
writer.WritePropertyName(propertyName);
writer.WriteRaw(exclusiveValue);
writer.WriteProperty(isExclusivePropertyName, true);
}
else if (!string.IsNullOrEmpty(value) && value is not null)

Check warning on line 371 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
// came from parsing an old document, we're just mirroring the information
writer.WritePropertyName(propertyName);
Expand Down Expand Up @@ -430,14 +430,19 @@
// required
writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) =>
{
if (!string.IsNullOrEmpty(s) && s is not null)

Check warning on line 433 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Change this condition so that it does not always evaluate to 'True'. (https://rules.sonarsource.com/csharp/RSPEC-2589)
{
w.WriteValue(s);
}
});

// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (nodeWriter, s) => nodeWriter.WriteAny(s));
var enumValue = (Enum == null || Enum.Count == 0)
&& !string.IsNullOrEmpty(Const)
&& version < OpenApiSpecVersion.OpenApi3_1
? new List<JsonNode> { JsonValue.Create(Const)! }
: Enum;
writer.WriteOptionalCollection(OpenApiConstants.Enum, enumValue, (nodeWriter, s) => nodeWriter.WriteAny(s));

// type
SerializeTypeProperty(writer, version);
Expand Down Expand Up @@ -618,7 +623,7 @@
/// <param name="writer">The open api writer.</param>
/// <param name="parentRequiredProperties">The list of required properties in parent schema.</param>
/// <param name="propertyName">The property name that will be serialized.</param>
private void SerializeAsV2(

Check warning on line 626 in src/Microsoft.OpenApi/Models/OpenApiSchema.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
IOpenApiWriter writer,
ISet<string>? parentRequiredProperties,
string? propertyName)
Expand Down Expand Up @@ -687,7 +692,10 @@
});

// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(s));
var enumValue = (Enum == null || Enum.Count == 0) && !string.IsNullOrEmpty(Const)
? new List<JsonNode> { JsonValue.Create(Const)! }
: Enum;
writer.WriteOptionalCollection(OpenApiConstants.Enum, enumValue, (nodeWriter, s) => nodeWriter.WriteAny(s));

// items
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w));
Expand Down
40 changes: 40 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,46 @@ public async Task WriteAsItemsPropertiesDoesNotWriteNull()
""";
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual)));
}
[Fact]
public async Task SerializeConstAsEnumV30()
{
// Arrange
var schema = new OpenApiSchema
{
Type = JsonSchemaType.String,
Const = "foo"
};


// Act
var actual = await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);

// Assert
var v3Node = JsonNode.Parse(actual);
Assert.NotNull(v3Node);
Assert.True(v3Node["enum"] is JsonArray singleEnum && singleEnum.Count == 1 && singleEnum[0]?.ToString() == "foo");
Assert.False(v3Node.AsObject().ContainsKey("const"));
}

[Fact]
public async Task SerializeConstAsEnumV20()
{
// Arrange
var schema = new OpenApiSchema
{
Type = JsonSchemaType.String,
Const = "foo"
};

// Act
var actual = await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi2_0);

// Assert
var v2Node = JsonNode.Parse(actual);
Assert.NotNull(v2Node);
Assert.True(v2Node["enum"] is JsonArray singleEnum && singleEnum.Count == 1 && singleEnum[0]?.ToString() == "foo");
Assert.False(v2Node.AsObject().ContainsKey("const"));
}


internal class SchemaVisitor : OpenApiVisitorBase
Expand Down
Loading