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
8 changes: 4 additions & 4 deletions src/Microsoft.OpenApi/Models/OpenApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;

Expand Down Expand Up @@ -45,7 +44,7 @@ public class OpenApiResponse : IOpenApiSerializable, IOpenApiReferenceable, IOpe
/// <summary>
/// Indicates if object is populated with data or is just a reference to the data
/// </summary>
public bool UnresolvedReference { get; set;}
public bool UnresolvedReference { get; set; }

/// <summary>
/// Reference pointer.
Expand Down Expand Up @@ -79,7 +78,7 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
writer.WriteStartObject();

// description
writer.WriteProperty(OpenApiConstants.Description, Description);
writer.WriteRequiredProperty(OpenApiConstants.Description, Description);

// headers
writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, (w, h) => h.SerializeAsV3(w));
Expand Down Expand Up @@ -123,7 +122,8 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
writer.WriteStartObject();

// description
writer.WriteProperty(OpenApiConstants.Description, Description);
writer.WriteRequiredProperty(OpenApiConstants.Description, Description);

if (Content != null)
{
var mediatype = Content.FirstOrDefault();
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ public static void WriteProperty(this IOpenApiWriter writer, string name, string
writer.WriteValue(value);
}

/// <summary>
/// Write required string property.
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="name">The property name.</param>
/// <param name="value">The property value.</param>
public static void WriteRequiredProperty(this IOpenApiWriter writer, string name, string value)
{
CheckArguments(writer, name);
writer.WritePropertyName(name);
if (value == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(value);
}
}

/// <summary>
/// Write a boolean property.
/// </summary>
Expand Down
10 changes: 7 additions & 3 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ public void SerializeOperationWithBodyAsV3JsonWorks()
""$ref"": ""#/components/responses/response1""
},
""400"": {
""description"": null,
""content"": {
""application/json"": {
""schema"": {
Expand Down Expand Up @@ -431,6 +432,7 @@ public void SerializeAdvancedOperationWithTagAndSecurityAsV3JsonWorks()
""$ref"": ""#/components/responses/response1""
},
""400"": {
""description"": null,
""content"": {
""application/json"": {
""schema"": {
Expand Down Expand Up @@ -554,7 +556,7 @@ public void SerializeOperationWithFormDataAsV3JsonWorks()

// Act
var actual = _operationWithFormData.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
Expand Down Expand Up @@ -607,7 +609,7 @@ public void SerializeOperationWithFormDataAsV2JsonWorks()

// Act
var actual = _operationWithFormData.SerializeAsJson(OpenApiSpecVersion.OpenApi2_0);

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
Expand Down Expand Up @@ -658,6 +660,7 @@ public void SerializeOperationWithBodyAsV2JsonWorks()
""$ref"": ""#/responses/response1""
},
""400"": {
""description"": null,
""schema"": {
""maximum"": 10,
""minimum"": 5,
Expand All @@ -672,7 +675,7 @@ public void SerializeOperationWithBodyAsV2JsonWorks()

// Act
var actual = _operationWithBody.SerializeAsJson(OpenApiSpecVersion.OpenApi2_0);

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
Expand Down Expand Up @@ -727,6 +730,7 @@ public void SerializeAdvancedOperationWithTagAndSecurityAsV2JsonWorks()
""$ref"": ""#/responses/response1""
},
""400"": {
""description"": null,
""schema"": {
""maximum"": 10,
""minimum"": 5,
Expand Down
11 changes: 9 additions & 2 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,18 @@ public void SerializeBasicResponseWorks(
OpenApiSpecVersion version,
OpenApiFormat format)
{
// Arrange & Act
// Arrange
var expected = format == OpenApiFormat.Json ? @"{
""description"": null
}" : @"description: ";

// Act
var actual = BasicResponse.Serialize(version, format);

// Assert
actual.Should().Be("{ }");
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
actual.Should().Be(expected);
}

[Fact]
Expand Down