Skip to content

Fix issue Empty tag causes error generating Kiota client #2283 #2286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2025
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
14 changes: 12 additions & 2 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ internal static partial class OpenApiV2Deserializer
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 null; // 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
.OfType<OpenApiTagReference>().ToList() is {Count: > 0} tags)
{
o.Tags = new HashSet<OpenApiTagReference>(tags, OpenApiTagComparer.Instance);
}
Expand Down
15 changes: 13 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 @@ -20,8 +21,18 @@ internal static partial class OpenApiV3Deserializer
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 null; // 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
.OfType<OpenApiTagReference>().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 @@ -17,8 +18,18 @@ internal static partial class OpenApiV31Deserializer
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 null; // 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
.OfType<OpenApiTagReference>().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."
}
}
}
}
}
}