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
20 changes: 2 additions & 18 deletions src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
return;
}
_tags = value is HashSet<OpenApiTagReference> tags && tags.Comparer is OpenApiTagComparer ?

Check warning on line 42 in src/Microsoft.OpenApi/Models/OpenApiOperation.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)

Check warning on line 42 in src/Microsoft.OpenApi/Models/OpenApiOperation.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)

Check warning on line 42 in src/Microsoft.OpenApi/Models/OpenApiOperation.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)

Check warning on line 42 in src/Microsoft.OpenApi/Models/OpenApiOperation.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)
tags :
new HashSet<OpenApiTagReference>(value, OpenApiTagComparer.Instance);
}
Expand Down Expand Up @@ -183,7 +183,7 @@
// tags
writer.WriteOptionalCollection(
OpenApiConstants.Tags,
VerifyTagReferences(Tags),
Tags,
callback);

// summary
Expand Down Expand Up @@ -228,7 +228,7 @@
/// <summary>
/// Serialize <see cref="OpenApiOperation"/> to Open Api v2.0.
/// </summary>
public virtual void SerializeAsV2(IOpenApiWriter writer)

Check warning on line 231 in src/Microsoft.OpenApi/Models/OpenApiOperation.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 231 in src/Microsoft.OpenApi/Models/OpenApiOperation.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 231 in src/Microsoft.OpenApi/Models/OpenApiOperation.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
Utils.CheckArgumentNull(writer);

Expand All @@ -237,7 +237,7 @@
// tags
writer.WriteOptionalCollection(
OpenApiConstants.Tags,
VerifyTagReferences(Tags),
Tags,
(w, t) => t.SerializeAsV2(w));

// summary
Expand Down Expand Up @@ -338,7 +338,7 @@

writer.WriteOptionalCollection(OpenApiConstants.Schemes, schemes, (w, s) =>
{
if (!string.IsNullOrEmpty(s) && s is not null)

Check warning on line 341 in src/Microsoft.OpenApi/Models/OpenApiOperation.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)

Check warning on line 341 in src/Microsoft.OpenApi/Models/OpenApiOperation.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)

Check warning on line 341 in src/Microsoft.OpenApi/Models/OpenApiOperation.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);
}
Expand All @@ -356,21 +356,5 @@

writer.WriteEndObject();
}

private static HashSet<OpenApiTagReference>? VerifyTagReferences(HashSet<OpenApiTagReference>? tags)
{
if (tags?.Count > 0)
{
foreach (var tag in tags)
{
if (tag.Target is null)
{
throw new OpenApiException($"The OpenAPI tag reference '{tag.Reference.Id}' does not reference a valid tag.");
}
}
}

return tags;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public string? Description
public Dictionary<string, IOpenApiExtension>? Extensions { get => Target?.Extensions; }

/// <inheritdoc/>
public string? Name { get => Target?.Name; }
public string? Name { get => Target?.Name ?? Reference?.Id; }
/// <inheritdoc/>
public override IOpenApiTag CopyReferenceAsTargetElementWithOverrides(IOpenApiTag source)
{
Expand Down
29 changes: 0 additions & 29 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,34 +860,5 @@ public void OpenApiOperationCopyConstructorWithAnnotationsSucceeds()

Assert.NotEqual(baseOperation.Metadata["key1"], actualOperation.Metadata["key1"]);
}

[Theory]
[InlineData(OpenApiSpecVersion.OpenApi2_0)]
[InlineData(OpenApiSpecVersion.OpenApi3_0)]
[InlineData(OpenApiSpecVersion.OpenApi3_1)]
public async Task SerializeAsJsonAsyncThrowsIfTagReferenceIsUnresolved(OpenApiSpecVersion version)
{
var document = new OpenApiDocument()
{
Tags =
[
new() { Name = "one" },
new() { Name = "three" }
]
};

var operation = new OpenApiOperation()
{
Tags =
[
new OpenApiTagReference("one", document),
new OpenApiTagReference("two", document),
new OpenApiTagReference("three", document)
]
};

var exception = await Assert.ThrowsAsync<OpenApiException>(() => operation.SerializeAsJsonAsync(version));
Assert.Equal("The OpenAPI tag reference 'two' does not reference a valid tag.", exception.Message);
}
}
}
Loading