Skip to content

Commit 9f5adb3

Browse files
committed
Added missing collection instantiations
1 parent 395bb87 commit 9f5adb3

File tree

7 files changed

+50
-2
lines changed

7 files changed

+50
-2
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
4141
/// <summary>
4242
/// An element to hold various schemas for the specification.
4343
/// </summary>
44-
public OpenApiComponents Components { get; set; }
44+
public OpenApiComponents Components { get; set; } = new OpenApiComponents();
4545

4646
/// <summary>
4747
/// A declaration of which security mechanisms can be used across the API.

src/Microsoft.OpenApi/Models/OpenApiPathItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class OpenApiPathItem : IOpenApiSerializable, IOpenApiExtensible
4646
/// <summary>
4747
/// This object MAY be extended with Specification Extensions.
4848
/// </summary>
49-
public IDictionary<string, IOpenApiAny> Extensions { get; set; }
49+
public IDictionary<string, IOpenApiAny> Extensions { get; set; } = new Dictionary<string, IOpenApiAny>();
5050

5151
/// <summary>
5252
/// Add one operation into this path item.

src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.Linq;
99
using Microsoft.OpenApi.Interfaces;
10+
using System.Collections;
1011

1112
namespace Microsoft.OpenApi.Writers
1213
{
@@ -123,6 +124,11 @@ public static void WriteOptionalObject<T>(
123124
{
124125
if (value != null)
125126
{
127+
IEnumerable values = value as IEnumerable;
128+
if (values != null && !values.GetEnumerator().MoveNext() )
129+
{
130+
return; // Don't render optional empty collections
131+
}
126132
writer.WriteRequiredObject(name, value, action);
127133
}
128134
}

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@
4747
<None Update="V2Tests\Samples\basic.v3.yaml">
4848
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4949
</None>
50+
<None Update="V2Tests\Samples\definitions.v3.yaml">
51+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
52+
</None>
5053
<None Update="V2Tests\Samples\host.v2.yaml">
5154
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5255
</None>
5356
<None Update="V2Tests\Samples\host.v3.yaml">
5457
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5558
</None>
59+
<None Update="V2Tests\Samples\definitions.v2.yaml">
60+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
61+
</None>
5662
<None Update="V2Tests\Samples\minimal.v3.yaml">
5763
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5864
</None>

test/Microsoft.OpenApi.Readers.Tests/V2Tests/ComparisonTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class ComparisonTests
1818
[Theory]
1919
[InlineData("minimal")]
2020
[InlineData("basic")]
21+
// [InlineData("definitions")] Currently broken due to V3 references not behaving the same as V2
2122
public void EquivalentV2AndV3DocumentsShouldProductEquivalentObjects(string fileName)
2223
{
2324
using (var streamV2 = File.OpenRead(Path.Combine(SampleFolderPath, $"{fileName}.v2.yaml")))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
swagger: 2.0
2+
info:
3+
title: This is a simple example
4+
version: 1.0.0
5+
paths:
6+
'/':
7+
get:
8+
produces:
9+
- application/json
10+
200:
11+
description: Ok
12+
schema:
13+
$ref: '#/definitions/simple'
14+
definitions:
15+
simple:
16+
type: string
17+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
openapi: 3.0
2+
info:
3+
title: This is a simple example
4+
version: 1.0.0
5+
paths:
6+
'/':
7+
get:
8+
200:
9+
description: Ok
10+
content:
11+
application/json:
12+
schema:
13+
$ref: '#/components/schemas/simple'
14+
components:
15+
schemas:
16+
simple:
17+
type: string
18+

0 commit comments

Comments
 (0)