Skip to content

Commit 06bc518

Browse files
authored
Merge pull request #436 from VitaliyKurokhtin/vvk/required
Always emitting description property for response object. It's required
2 parents abfebb1 + 92ba7a3 commit 06bc518

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

src/Microsoft.OpenApi/Models/OpenApiResponse.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Collections.Generic;
55
using System.Linq;
6-
using Microsoft.OpenApi.Any;
76
using Microsoft.OpenApi.Interfaces;
87
using Microsoft.OpenApi.Writers;
98

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

5049
/// <summary>
5150
/// Reference pointer.
@@ -79,7 +78,7 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
7978
writer.WriteStartObject();
8079

8180
// description
82-
writer.WriteProperty(OpenApiConstants.Description, Description);
81+
writer.WriteRequiredProperty(OpenApiConstants.Description, Description);
8382

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

125124
// description
126-
writer.WriteProperty(OpenApiConstants.Description, Description);
125+
writer.WriteRequiredProperty(OpenApiConstants.Description, Description);
126+
127127
if (Content != null)
128128
{
129129
var mediatype = Content.FirstOrDefault();

src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ public static void WriteProperty(this IOpenApiWriter writer, string name, string
3232
writer.WriteValue(value);
3333
}
3434

35+
/// <summary>
36+
/// Write required string property.
37+
/// </summary>
38+
/// <param name="writer">The writer.</param>
39+
/// <param name="name">The property name.</param>
40+
/// <param name="value">The property value.</param>
41+
public static void WriteRequiredProperty(this IOpenApiWriter writer, string name, string value)
42+
{
43+
CheckArguments(writer, name);
44+
writer.WritePropertyName(name);
45+
if (value == null)
46+
{
47+
writer.WriteNull();
48+
}
49+
else
50+
{
51+
writer.WriteValue(value);
52+
}
53+
}
54+
3555
/// <summary>
3656
/// Write a boolean property.
3757
/// </summary>

test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ public void SerializeOperationWithBodyAsV3JsonWorks()
359359
""$ref"": ""#/components/responses/response1""
360360
},
361361
""400"": {
362+
""description"": null,
362363
""content"": {
363364
""application/json"": {
364365
""schema"": {
@@ -431,6 +432,7 @@ public void SerializeAdvancedOperationWithTagAndSecurityAsV3JsonWorks()
431432
""$ref"": ""#/components/responses/response1""
432433
},
433434
""400"": {
435+
""description"": null,
434436
""content"": {
435437
""application/json"": {
436438
""schema"": {
@@ -554,7 +556,7 @@ public void SerializeOperationWithFormDataAsV3JsonWorks()
554556

555557
// Act
556558
var actual = _operationWithFormData.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
557-
559+
558560
// Assert
559561
actual = actual.MakeLineBreaksEnvironmentNeutral();
560562
expected = expected.MakeLineBreaksEnvironmentNeutral();
@@ -607,7 +609,7 @@ public void SerializeOperationWithFormDataAsV2JsonWorks()
607609

608610
// Act
609611
var actual = _operationWithFormData.SerializeAsJson(OpenApiSpecVersion.OpenApi2_0);
610-
612+
611613
// Assert
612614
actual = actual.MakeLineBreaksEnvironmentNeutral();
613615
expected = expected.MakeLineBreaksEnvironmentNeutral();
@@ -658,6 +660,7 @@ public void SerializeOperationWithBodyAsV2JsonWorks()
658660
""$ref"": ""#/responses/response1""
659661
},
660662
""400"": {
663+
""description"": null,
661664
""schema"": {
662665
""maximum"": 10,
663666
""minimum"": 5,
@@ -672,7 +675,7 @@ public void SerializeOperationWithBodyAsV2JsonWorks()
672675

673676
// Act
674677
var actual = _operationWithBody.SerializeAsJson(OpenApiSpecVersion.OpenApi2_0);
675-
678+
676679
// Assert
677680
actual = actual.MakeLineBreaksEnvironmentNeutral();
678681
expected = expected.MakeLineBreaksEnvironmentNeutral();
@@ -727,6 +730,7 @@ public void SerializeAdvancedOperationWithTagAndSecurityAsV2JsonWorks()
727730
""$ref"": ""#/responses/response1""
728731
},
729732
""400"": {
733+
""description"": null,
730734
""schema"": {
731735
""maximum"": 10,
732736
""minimum"": 5,

test/Microsoft.OpenApi.Tests/Models/OpenApiResponseTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,18 @@ public void SerializeBasicResponseWorks(
116116
OpenApiSpecVersion version,
117117
OpenApiFormat format)
118118
{
119-
// Arrange & Act
119+
// Arrange
120+
var expected = format == OpenApiFormat.Json ? @"{
121+
""description"": null
122+
}" : @"description: ";
123+
124+
// Act
120125
var actual = BasicResponse.Serialize(version, format);
121126

122127
// Assert
123-
actual.Should().Be("{ }");
128+
actual = actual.MakeLineBreaksEnvironmentNeutral();
129+
expected = expected.MakeLineBreaksEnvironmentNeutral();
130+
actual.Should().Be(expected);
124131
}
125132

126133
[Fact]

0 commit comments

Comments
 (0)