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
22 changes: 18 additions & 4 deletions src/Microsoft.OpenApi/Models/OpenApiTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ internal override void WriteAsV3(IOpenApiWriter writer)
}

writer.WriteStartObject();
writer.WriteStringProperty("name", Name);
writer.WriteStringProperty("description", Description);

writer.WriteStringProperty(OpenApiConstants.OpenApiDocName, Name);

writer.WriteStringProperty(OpenApiConstants.OpenApiDocDescription, Description);

writer.WriteObject(OpenApiConstants.OpenApiDocExternalDocs, ExternalDocs, (w, e) => e.WriteAsV3(w));

writer.WriteExtensions(Extensions);

writer.WriteEndObject();
}

Expand All @@ -67,8 +74,15 @@ internal override void WriteAsV2(IOpenApiWriter writer)
}

writer.WriteStartObject();
writer.WriteStringProperty("name", Name);
writer.WriteStringProperty("description", Description);

writer.WriteStringProperty(OpenApiConstants.OpenApiDocName, Name);

writer.WriteStringProperty(OpenApiConstants.OpenApiDocDescription, Description);

writer.WriteObject(OpenApiConstants.OpenApiDocExternalDocs, ExternalDocs, (w, e) => e.WriteAsV2(w));

writer.WriteExtensions(Extensions);

writer.WriteEndObject();
}
}
Expand Down
70 changes: 69 additions & 1 deletion test/Microsoft.OpenApi.Tests/Models/OpenApiTagTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Xunit;

namespace Microsoft.OpenApi.Models.Tests
{
public class OpenApiTagTests
Expand All @@ -12,7 +16,71 @@ public class OpenApiTagTests
{
Name = "pet",
Description = "Pets operations",
ExternalDocs = OpenApiExternalDocsTests.AdvanceExDocs
ExternalDocs = OpenApiExternalDocsTests.AdvanceExDocs,
Extensions = new Dictionary<string, IOpenApiAny>
{
{ "x-tag-extension", new OpenApiNull() }
}
};

[Theory]
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json, "{ }")]
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json, "{ }")]
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Yaml, "")]
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Yaml, "")]
public void SerializeBasicTagWorks(OpenApiSpecVersion version,
OpenApiFormat format, string expect)
{
// Arrange & Act
string actual = BasicTag.Serialize(version, format);

// Assert
Assert.Equal(expect, actual);
}

[Theory]
[InlineData(OpenApiSpecVersion.OpenApi3_0)]
[InlineData(OpenApiSpecVersion.OpenApi2_0)]
public void SerializeAdvanceTagAsJsonWorks(OpenApiSpecVersion version)
{
// Arrange
string expect = @"
{
""name"": ""pet"",
""description"": ""Pets operations"",
""externalDocs"": {
""description"": ""Find more info here"",
""url"": ""https://example.com""
},
""x-tag-extension"": null
}";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this in the Data too (so repeat this twice, once for 3.0 and once for 2.0). Even though 3.0 and 2.0 serialize to the same string, this is more of a circumstantial thing rather than something ingrained in the semantics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to leave it unchanged until we move these into a file.

// Act
string actual = AdvanceTag.SerializeAsJson(version);

// Assert
Assert.Equal(expect, actual);
}

[Theory]
[InlineData(OpenApiSpecVersion.OpenApi3_0)]
[InlineData(OpenApiSpecVersion.OpenApi2_0)]
public void SerializeAdvanceTagAsYamlWorks(OpenApiSpecVersion version)
{
// Arrange
string expect = @"
name: pet
description: Pets operations
externalDocs:
description: Find more info here
url: https://example.com
x-tag-extension: ";

// Act
string actual = AdvanceTag.SerializeAsYaml(version);

// Assert
Assert.Equal(expect, actual);
}
}
}