Skip to content

Commit b8df4c2

Browse files
authored
Null spec version should not cause null reference error. (#97)
- Null spec version should not cause null reference error. - Alternative is to assign the value as 3.0.0 and 2.0 correctly, but then we are being a little inconsistent with default values again. - Only null string should not be written. Empty string may(?) have different semantic value.
1 parent 097b7f3 commit b8df4c2

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace Microsoft.OpenApi.Models
1818
public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
1919
{
2020
/// <summary>
21-
/// REQUIRED.This string MUST be the semantic version number of the OpenAPI Specification version that the OpenAPI document uses.
21+
/// REQUIRED. This string MUST be the semantic version number of the OpenAPI Specification version
22+
/// that the OpenAPI document uses.
2223
/// </summary>
2324
public Version SpecVersion { get; set; }
2425

@@ -75,7 +76,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
7576
writer.WriteStartObject();
7677

7778
// openapi
78-
writer.WriteProperty(OpenApiConstants.OpenApi, SpecVersion.ToString());
79+
writer.WriteProperty(OpenApiConstants.OpenApi, SpecVersion?.ToString());
7980

8081
// info
8182
writer.WriteRequiredObject(OpenApiConstants.Info, Info, (w, i) => i.SerializeAsV3(w));
@@ -117,7 +118,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
117118
writer.WriteStartObject();
118119

119120
// swagger
120-
writer.WriteProperty(OpenApiConstants.Swagger, SpecVersion.ToString());
121+
writer.WriteProperty(OpenApiConstants.Swagger, SpecVersion?.ToString());
121122

122123
// info
123124
writer.WriteRequiredObject(OpenApiConstants.Info, Info, (w, i) => i.SerializeAsV2(w));
@@ -165,7 +166,7 @@ private static void WriteHostInfoV2(IOpenApiWriter writer, IList<OpenApiServer>
165166
// Arbitrarily choose the first server given that V2 only allows
166167
// one host, port, and base path.
167168
var firstServer = servers.First();
168-
169+
169170
// Divide the URL in the Url property into host and basePath required in OpenAPI V2
170171
// The Url property cannotcontain path templating to be valid for V2 serialization.
171172
var firstServerUrl = new Uri(firstServer.Url);

src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static class OpenApiWriterExtensions
2323
/// <param name="value">The property value.</param>
2424
public static void WriteProperty(this IOpenApiWriter writer, string name, string value)
2525
{
26-
if (string.IsNullOrEmpty(value))
26+
if (value == null)
2727
{
2828
return;
2929
}

0 commit comments

Comments
 (0)