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
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
ReportMissing(openApiNode, required);

// Post Process OpenApi Object
if (openApidoc.Servers == null)
{
openApidoc.Servers = new List<OpenApiServer>();
}

MakeServers(openApidoc.Servers, openApiNode.Context);

return openApidoc;
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class OpenApiDocument : OpenApiElement, IOpenApiExtension
/// <summary>
/// An array of Server Objects, which provide connectivity information to a target server.
/// </summary>
public IList<OpenApiServer> Servers { get; set; } = new List<OpenApiServer>();
public IList<OpenApiServer> Servers { get; set; }

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

/// <summary>
/// Additional external documentation.
/// </summary>
public OpenApiExternalDocs ExternalDocs { get; set; } = new OpenApiExternalDocs();
public OpenApiExternalDocs ExternalDocs { get; set; }

/// <summary>
/// This object MAY be extended with Specification Extensions.
Expand Down
12 changes: 8 additions & 4 deletions src/Microsoft.OpenApi/Services/OpenApiWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ public void Walk(OpenApiDocument doc)
this._visitor.Visit(doc);
this._visitor.Visit(doc.Info);
this._visitor.Visit(doc.Servers);
foreach (var server in doc.Servers)

if (doc.Servers != null)
{
this._visitor.Visit(server);
foreach (var variable in server.Variables.Values)
foreach (var server in doc.Servers)
{
this._visitor.Visit(variable);
this._visitor.Visit(server);
foreach (var variable in server.Variables.Values)
{
this._visitor.Visit(variable);
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ public static void WriteRequiredObject<T>(this IOpenApiWriter writer, string nam
CheckArguments(writer, name, action);

writer.WritePropertyName(name);
action(writer, value);
if (value != null)
{
action(writer, value);
}
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion test/Microsoft.OpenApi.Readers.Tests/ModelToV2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ private static JObject ExportV2ToJObject(OpenApiDocument openApiDoc)
[Fact]
public void HostTest()
{
var openApiDoc = new OpenApiDocument();
var openApiDoc = new OpenApiDocument
{
Servers = new List<OpenApiServer>()
};

openApiDoc.Servers.Add(new OpenApiServer {Url = "http://example.org/api"});
openApiDoc.Servers.Add(new OpenApiServer {Url = "https://example.org/api"});

Expand Down