Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
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
10 changes: 8 additions & 2 deletions 10 src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,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 270 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 21 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
Utils.CheckArgumentNull(schema);
Title = schema.Title ?? Title;
Expand Down Expand Up @@ -348,23 +348,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 351 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 351 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 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(exclusivePropertyName);
writer.WriteRaw(exclusiveValue);
}
else if (isExclusiveValue == true && !string.IsNullOrEmpty(value) && value is not null)

Check warning on line 361 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 367 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 @@ -373,14 +373,14 @@
}
else
{
if (!string.IsNullOrEmpty(exclusiveValue) && exclusiveValue is not null)

Check warning on line 376 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 383 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 All @@ -391,7 +391,7 @@
}
}

private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,

Check warning on line 394 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)
Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
writer.WriteStartObject();
Expand Down Expand Up @@ -686,7 +686,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 689 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 18 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
IOpenApiWriter writer,
ISet<string>? parentRequiredProperties,
string? propertyName)
Expand Down Expand Up @@ -790,9 +790,15 @@
});

// additionalProperties
// a schema cannot be serialized in v2
// true is the default, no need to write it out
if (!AdditionalPropertiesAllowed)
if (AdditionalProperties is not null)
{
writer.WriteOptionalObject(
OpenApiConstants.AdditionalProperties,
AdditionalProperties,
(w, s) => s.SerializeAsV2(w));
}
else if (!AdditionalPropertiesAllowed)
{
writer.WriteProperty(OpenApiConstants.AdditionalProperties, AdditionalPropertiesAllowed);
}
Expand Down
22 changes: 20 additions & 2 deletions 22 test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,9 @@ public async Task SerializeConstAsEnumV20()
}

[Fact]
public async Task SerializeAdditionalPropertiesAsV2DoesNotEmit()
public async Task SerializeAdditionalPropertiesAsV2WithEmptySchemaEmits()
{
var expected = @"{ }";
var expected = @"{ ""additionalProperties"": { } }";
// Given
var schema = new OpenApiSchema
{
Expand All @@ -734,6 +734,24 @@ public async Task SerializeAdditionalPropertiesAsV2DoesNotEmit()
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual)));
}

[Fact]
public async Task SerializeAdditionalPropertiesAsV2WithRefSchemaEmits()
{
var expected = @"{ ""type"": ""object"", ""additionalProperties"": { ""$ref"": ""#/definitions/MyModel"" } }";
// Given - schema with additionalProperties pointing to a ref (dictionary case)
var schema = new OpenApiSchema
{
Type = JsonSchemaType.Object,
AdditionalProperties = new OpenApiSchemaReference("MyModel", null)
};

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

// Then
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual)));
}

[Fact]
public async Task SerializeAdditionalPropertiesAllowedAsV2DefaultDoesNotEmit()
{
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.