Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 16 additions & 2 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@
/// </summary>
internal static partial class OpenApiV2Deserializer
{
/// <summary>
/// Have a default empty tag we can use to filter out empty tags.
/// </summary>
private static OpenApiTagReference emptyTagReference = new("empty");
private static readonly FixedFieldMap<OpenApiOperation> _operationFixedFields =
new()
{
{
"tags", (o, n, doc) => {
if (n.CreateSimpleList((valueNode, doc) => LoadTagByReference(valueNode.GetScalarValue(), doc), doc) is {Count: > 0} tags)
"tags", (o, n, doc) => {
if (n.CreateSimpleList(
(valueNode, doc) =>
{
var val = valueNode.GetScalarValue();
if (string.IsNullOrEmpty(val))
return emptyTagReference; // Avoid exception on empty tag, we'll remove these from the list further on
return LoadTagByReference(val , doc);
},
doc)
// Filter out empty tags instead of excepting on them
.Where(n => !object.ReferenceEquals(emptyTagReference, n)).ToList() is {Count: > 0} tags)
{
o.Tags = new HashSet<OpenApiTagReference>(tags, OpenApiTagComparer.Instance);
}
Expand Down
19 changes: 17 additions & 2 deletions src/Microsoft.OpenApi/Reader/V3/OpenApiOperationDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.References;
Expand All @@ -16,12 +17,26 @@
/// </summary>
internal static partial class OpenApiV3Deserializer
{
/// <summary>
/// Have a default empty tag we can use to filter out empty tags.
/// </summary>
private static OpenApiTagReference emptyTagReference = new("empty");
private static readonly FixedFieldMap<OpenApiOperation> _operationFixedFields =
new()
{
{
"tags", (o, n, doc) => {
if (n.CreateSimpleList((valueNode, doc) => LoadTagByReference(valueNode.GetScalarValue(), doc), doc) is {Count: > 0} tags)
"tags", (o, n, doc) => {
if (n.CreateSimpleList(
(valueNode, doc) =>
{
var val = valueNode.GetScalarValue();
if (string.IsNullOrEmpty(val))
return emptyTagReference; // Avoid exception on empty tag, we'll remove these from the list further on
return LoadTagByReference(val , doc);
},
doc)
// Filter out empty tags instead of excepting on them
.Where(n => !object.ReferenceEquals(emptyTagReference, n)).ToList() is {Count: > 0} tags)
{
o.Tags = new HashSet<OpenApiTagReference>(tags, OpenApiTagComparer.Instance);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.References;
Expand All @@ -13,12 +14,26 @@
/// </summary>
internal static partial class OpenApiV31Deserializer
{
/// <summary>
/// Have a default empty tag we can use to filter out empty tags.
/// </summary>
private static OpenApiTagReference emptyTagReference = new("empty");
private static readonly FixedFieldMap<OpenApiOperation> _operationFixedFields =
new()
{
{
"tags", (o, n, doc) => {
if (n.CreateSimpleList((valueNode, doc) => LoadTagByReference(valueNode.GetScalarValue(), doc), doc) is {Count: > 0} tags)
"tags", (o, n, doc) => {
if (n.CreateSimpleList(
(valueNode, doc) =>
{
var val = valueNode.GetScalarValue();
if (string.IsNullOrEmpty(val))
return emptyTagReference; // Avoid exception on empty tag, we'll remove these from the list further on
return LoadTagByReference(val , doc);
},
doc)
// Filter out empty tags instead of excepting on them
.Where(n => !object.ReferenceEquals(emptyTagReference, n)).ToList() is {Count: > 0} tags)
{
o.Tags = new HashSet<OpenApiTagReference>(tags, OpenApiTagComparer.Instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ public async Task ParseDocumentWith31PropertiesWorks()
await Verifier.Verify(actual);
}

[Fact]
public async Task ParseDocumentWithEmptyTagsWorks()
{
var path = Path.Combine(SampleFolderPath, "documentWithEmptyTags.json");
var doc = (await OpenApiDocument.LoadAsync(path, SettingsFixture.ReaderSettings)).Document;

doc.Paths["/groups"].Operations[HttpMethod.Get].Tags.Should().BeNull("Empty tags are ignored, so we should not have any tags");
}

[Fact]
public void ParseEmptyMemoryStreamThrowsAnArgumentException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"openapi": "3.1.0",
"info": {
"description": "Groups API",
"title": "Groups",
"version": "1.0"
},
"paths": {
"/groups": {
"get": {
"operationId": "getGroups",
"parameters": [
{
"description": "Zero-based page index (0..N)",
"example": 0,
"in": "query",
"name": "page",
"required": false,
"schema": {
"type": "integer",
"format": "int32",
"default": 0,
"minimum": 0
}
}
],
"responses": {
"200": {
"content": { "application/json": { "schema": { "$ref": "#/components/schemas/PaginatedGroup" } } }
}
},
"tags": [ "" ]
}
}
},
"components": {
"schemas": {
"PaginatedGroup": {
"type": "object",
"properties": {
"number": {
"type": "integer",
"format": "int32",
"description": "The number of the current page."
}
}
}
}
}
}