Skip to content

Commit 8f6b612

Browse files
committed
remove optional properties initializer
1 parent 530cf62 commit 8f6b612

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
9393
ReportMissing(openApiNode, required);
9494

9595
// Post Process OpenApi Object
96+
if (openApidoc.Servers == null)
97+
{
98+
openApidoc.Servers = new List<OpenApiServer>();
99+
}
100+
96101
MakeServers(openApidoc.Servers, openApiNode.Context);
97102

98103
return openApidoc;

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class OpenApiDocument : OpenApiElement, IOpenApiExtension
3030
/// <summary>
3131
/// An array of Server Objects, which provide connectivity information to a target server.
3232
/// </summary>
33-
public IList<OpenApiServer> Servers { get; set; } = new List<OpenApiServer>();
33+
public IList<OpenApiServer> Servers { get; set; }
3434

3535
/// <summary>
3636
/// REQUIRED. The available paths and operations for the API.
@@ -50,12 +50,12 @@ public class OpenApiDocument : OpenApiElement, IOpenApiExtension
5050
/// <summary>
5151
/// A list of tags used by the specification with additional metadata.
5252
/// </summary>
53-
public IList<OpenApiTag> Tags { get; set; } = new List<OpenApiTag>();
53+
public IList<OpenApiTag> Tags { get; set; }
5454

5555
/// <summary>
5656
/// Additional external documentation.
5757
/// </summary>
58-
public OpenApiExternalDocs ExternalDocs { get; set; } = new OpenApiExternalDocs();
58+
public OpenApiExternalDocs ExternalDocs { get; set; }
5959

6060
/// <summary>
6161
/// This object MAY be extended with Specification Extensions.

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ public void Walk(OpenApiDocument doc)
3232
this._visitor.Visit(doc);
3333
this._visitor.Visit(doc.Info);
3434
this._visitor.Visit(doc.Servers);
35-
foreach (var server in doc.Servers)
35+
36+
if (doc.Servers != null)
3637
{
37-
this._visitor.Visit(server);
38-
foreach (var variable in server.Variables.Values)
38+
foreach (var server in doc.Servers)
3939
{
40-
this._visitor.Visit(variable);
40+
this._visitor.Visit(server);
41+
foreach (var variable in server.Variables.Values)
42+
{
43+
this._visitor.Visit(variable);
44+
}
4145
}
4246
}
4347

src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ public static void WriteRequiredObject<T>(this IOpenApiWriter writer, string nam
133133
CheckArguments(writer, name, action);
134134

135135
writer.WritePropertyName(name);
136-
action(writer, value);
136+
if (value != null)
137+
{
138+
action(writer, value);
139+
}
137140
}
138141

139142
/// <summary>

test/Microsoft.OpenApi.Readers.Tests/ModelToV2Tests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ private static JObject ExportV2ToJObject(OpenApiDocument openApiDoc)
4242
[Fact]
4343
public void HostTest()
4444
{
45-
var openApiDoc = new OpenApiDocument();
45+
var openApiDoc = new OpenApiDocument
46+
{
47+
Servers = new List<OpenApiServer>()
48+
};
49+
4650
openApiDoc.Servers.Add(new OpenApiServer {Url = "http://example.org/api"});
4751
openApiDoc.Servers.Add(new OpenApiServer {Url = "https://example.org/api"});
4852

0 commit comments

Comments
 (0)