Skip to content

Commit 521d636

Browse files
fix: Empty tag causes error generating Kiota client #2283 (#2286)
* Fix issue Empty tag causes error generating Kiota client #2283 * fix: empty tag causes Exception generating Kiota client #2283 * fix: empty tag causes Exception generating Kiota client #2283
1 parent 5d99cc4 commit 521d636

File tree

5 files changed

+97
-6
lines changed

5 files changed

+97
-6
lines changed

src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,18 @@ internal static partial class OpenApiV2Deserializer
2323
new()
2424
{
2525
{
26-
"tags", (o, n, doc) => {
27-
if (n.CreateSimpleList((valueNode, doc) => LoadTagByReference(valueNode.GetScalarValue(), doc), doc) is {Count: > 0} tags)
26+
"tags", (o, n, doc) => {
27+
if (n.CreateSimpleList(
28+
(valueNode, doc) =>
29+
{
30+
var val = valueNode.GetScalarValue();
31+
if (string.IsNullOrEmpty(val))
32+
return null; // Avoid exception on empty tag, we'll remove these from the list further on
33+
return LoadTagByReference(val , doc);
34+
},
35+
doc)
36+
// Filter out empty tags instead of excepting on them
37+
.OfType<OpenApiTagReference>().ToList() is {Count: > 0} tags)
2838
{
2939
o.Tags = new HashSet<OpenApiTagReference>(tags, OpenApiTagComparer.Instance);
3040
}

src/Microsoft.OpenApi/Reader/V3/OpenApiOperationDeserializer.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using Microsoft.OpenApi.Extensions;
78
using Microsoft.OpenApi.Models;
89
using Microsoft.OpenApi.Models.References;
@@ -20,8 +21,18 @@ internal static partial class OpenApiV3Deserializer
2021
new()
2122
{
2223
{
23-
"tags", (o, n, doc) => {
24-
if (n.CreateSimpleList((valueNode, doc) => LoadTagByReference(valueNode.GetScalarValue(), doc), doc) is {Count: > 0} tags)
24+
"tags", (o, n, doc) => {
25+
if (n.CreateSimpleList(
26+
(valueNode, doc) =>
27+
{
28+
var val = valueNode.GetScalarValue();
29+
if (string.IsNullOrEmpty(val))
30+
return null; // Avoid exception on empty tag, we'll remove these from the list further on
31+
return LoadTagByReference(val , doc);
32+
},
33+
doc)
34+
// Filter out empty tags instead of excepting on them
35+
.OfType<OpenApiTagReference>().ToList() is {Count: > 0} tags)
2536
{
2637
o.Tags = new HashSet<OpenApiTagReference>(tags, OpenApiTagComparer.Instance);
2738
}

src/Microsoft.OpenApi/Reader/V31/OpenApiOperationDeserializer.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Microsoft.OpenApi.Extensions;
45
using Microsoft.OpenApi.Models;
56
using Microsoft.OpenApi.Models.References;
@@ -17,8 +18,18 @@ internal static partial class OpenApiV31Deserializer
1718
new()
1819
{
1920
{
20-
"tags", (o, n, doc) => {
21-
if (n.CreateSimpleList((valueNode, doc) => LoadTagByReference(valueNode.GetScalarValue(), doc), doc) is {Count: > 0} tags)
21+
"tags", (o, n, doc) => {
22+
if (n.CreateSimpleList(
23+
(valueNode, doc) =>
24+
{
25+
var val = valueNode.GetScalarValue();
26+
if (string.IsNullOrEmpty(val))
27+
return null; // Avoid exception on empty tag, we'll remove these from the list further on
28+
return LoadTagByReference(val , doc);
29+
},
30+
doc)
31+
// Filter out empty tags instead of excepting on them
32+
.OfType<OpenApiTagReference>().ToList() is {Count: > 0} tags)
2233
{
2334
o.Tags = new HashSet<OpenApiTagReference>(tags, OpenApiTagComparer.Instance);
2435
}

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs

+9
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,15 @@ public async Task ParseDocumentWith31PropertiesWorks()
570570
await Verifier.Verify(actual);
571571
}
572572

573+
[Fact]
574+
public async Task ParseDocumentWithEmptyTagsWorks()
575+
{
576+
var path = Path.Combine(SampleFolderPath, "documentWithEmptyTags.json");
577+
var doc = (await OpenApiDocument.LoadAsync(path, SettingsFixture.ReaderSettings)).Document;
578+
579+
doc.Paths["/groups"].Operations[HttpMethod.Get].Tags.Should().BeNull("Empty tags are ignored, so we should not have any tags");
580+
}
581+
573582
[Fact]
574583
public void ParseEmptyMemoryStreamThrowsAnArgumentException()
575584
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"description": "Groups API",
5+
"title": "Groups",
6+
"version": "1.0"
7+
},
8+
"paths": {
9+
"/groups": {
10+
"get": {
11+
"operationId": "getGroups",
12+
"parameters": [
13+
{
14+
"description": "Zero-based page index (0..N)",
15+
"example": 0,
16+
"in": "query",
17+
"name": "page",
18+
"required": false,
19+
"schema": {
20+
"type": "integer",
21+
"format": "int32",
22+
"default": 0,
23+
"minimum": 0
24+
}
25+
}
26+
],
27+
"responses": {
28+
"200": {
29+
"content": { "application/json": { "schema": { "$ref": "#/components/schemas/PaginatedGroup" } } }
30+
}
31+
},
32+
"tags": [ "" ]
33+
}
34+
}
35+
},
36+
"components": {
37+
"schemas": {
38+
"PaginatedGroup": {
39+
"type": "object",
40+
"properties": {
41+
"number": {
42+
"type": "integer",
43+
"format": "int32",
44+
"description": "The number of the current page."
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)