Skip to content

Commit 0335675

Browse files
authored
Merge branch 'master' into dm/OpenApiAny
2 parents b20677a + 896e874 commit 0335675

File tree

10 files changed

+77
-36
lines changed

10 files changed

+77
-36
lines changed

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ internal static partial class OpenApiV2Deserializer
1919
{
2020
public static FixedFieldMap<OpenApiDocument> OpenApiFixedFields = new FixedFieldMap<OpenApiDocument>
2121
{
22-
{
23-
"swagger", (o, n) =>
24-
{
25-
o.SpecVersion = new Version(2, 0);
26-
}
27-
},
22+
{"swagger", (o, n) => { } /* Version is valid field but we already parsed it */ },
2823
{"info", (o, n) => o.Info = LoadInfo(n)},
2924
{"host", (o, n) => n.Context.SetTempStorage("host", n.GetScalarValue())},
3025
{"basePath", (o, n) => n.Context.SetTempStorage("basePath", n.GetScalarValue())},
@@ -45,20 +40,36 @@ internal static partial class OpenApiV2Deserializer
4540
{"paths", (o, n) => o.Paths = LoadPaths(n)},
4641
{
4742
"definitions",
48-
(o, n) => o.Components.Schemas = n.CreateMapWithReference(
43+
(o, n) => {
44+
o.Components = new OpenApiComponents();
45+
o.Components.Schemas = n.CreateMapWithReference(
4946
ReferenceType.Schema,
5047
"#/definitions/",
51-
LoadSchema)
48+
LoadSchema);
49+
}
5250
},
5351
{
5452
"parameters",
55-
(o, n) => o.Components.Parameters = n.CreateMapWithReference(
53+
(o, n) => {
54+
o.Components = new OpenApiComponents();
55+
o.Components.Parameters = n.CreateMapWithReference(
5656
ReferenceType.Parameter,
5757
"#/parameters/",
58-
LoadParameter)
58+
LoadParameter);
59+
}
60+
},
61+
{
62+
"responses", (o, n) => {
63+
o.Components = new OpenApiComponents();
64+
o.Components.Responses = n.CreateMap(LoadResponse);
65+
}
66+
},
67+
{
68+
"securityDefinitions", (o, n) => {
69+
o.Components = new OpenApiComponents();
70+
o.Components.SecuritySchemes = n.CreateMap(LoadSecurityScheme);
71+
}
5972
},
60-
{"responses", (o, n) => o.Components.Responses = n.CreateMap(LoadResponse)},
61-
{"securityDefinitions", (o, n) => o.Components.SecuritySchemes = n.CreateMap(LoadSecurityScheme)},
6273
{"security", (o, n) => o.SecurityRequirements = n.CreateList(LoadSecurityRequirement)},
6374
{"tags", (o, n) => o.Tags = n.CreateList(LoadTag)},
6475
{"externalDocs", (o, n) => o.ExternalDocs = LoadExternalDocs(n)}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ internal static partial class OpenApiV3Deserializer
1919
{
2020
public static FixedFieldMap<OpenApiDocument> OpenApiFixedFields = new FixedFieldMap<OpenApiDocument>
2121
{
22-
{
23-
"openapi", (o, n) =>
24-
{
25-
o.SpecVersion = new Version(n.GetScalarValue());
26-
}
27-
},
22+
{"openapi", (o, n) => { } /* Version is valid field but we already parsed it */ },
2823
{"info", (o, n) => o.Info = LoadInfo(n)},
2924
{"servers", (o, n) => o.Servers = n.CreateList(LoadServer)},
3025
{"paths", (o, n) => o.Paths = LoadPaths(n)},

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ namespace Microsoft.OpenApi.Models
1717
/// </summary>
1818
public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
1919
{
20-
/// <summary>
21-
/// REQUIRED. This string MUST be the semantic version number of the OpenAPI Specification version
22-
/// that the OpenAPI document uses.
23-
/// </summary>
24-
public Version SpecVersion { get; set; }
2520

2621
/// <summary>
2722
/// REQUIRED. Provides metadata about the API. The metadata MAY be used by tooling as required.
@@ -64,7 +59,7 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
6459
public IDictionary<string, IOpenApiAny> Extensions { get; set; } = new Dictionary<string, IOpenApiAny>();
6560

6661
/// <summary>
67-
/// Serialize <see cref="OpenApiDocument"/> to Open Api v3.0.
62+
/// Serialize <see cref="OpenApiDocument"/> to Open Api v3.0.0
6863
/// </summary>
6964
public void SerializeAsV3(IOpenApiWriter writer)
7065
{
@@ -76,7 +71,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
7671
writer.WriteStartObject();
7772

7873
// openapi
79-
writer.WriteProperty(OpenApiConstants.OpenApi, SpecVersion?.ToString());
74+
writer.WriteProperty(OpenApiConstants.OpenApi, "3.0.0");
8075

8176
// info
8277
writer.WriteRequiredObject(OpenApiConstants.Info, Info, (w, i) => i.SerializeAsV3(w));
@@ -118,7 +113,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
118113
writer.WriteStartObject();
119114

120115
// swagger
121-
writer.WriteProperty(OpenApiConstants.Swagger, SpecVersion?.ToString());
116+
writer.WriteProperty(OpenApiConstants.Swagger, "2.0");
122117

123118
// info
124119
writer.WriteRequiredObject(OpenApiConstants.Info, Info, (w, i) => i.SerializeAsV2(w));

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: 2 additions & 3 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")))
@@ -29,9 +30,7 @@ public void EquivalentV2AndV3DocumentsShouldProductEquivalentObjects(string file
2930
// Everything in the DOM read from V2 and V3 documents should be equal
3031
// except the SpecVersion property (2.0 and 3.0.0)
3132
openApiDocV3.ShouldBeEquivalentTo(
32-
openApiDocV2,
33-
options => options.Excluding(
34-
s => s.SelectedMemberPath == nameof(OpenApiDocument.SpecVersion)));
33+
openApiDocV2);
3534

3635
contextV2.ShouldBeEquivalentTo(contextV3);
3736
}
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+

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public void ParseDocumentFromInlineStringShouldSucceed()
4040

4141
openApiDoc.ShouldBeEquivalentTo(new OpenApiDocument()
4242
{
43-
SpecVersion = new Version(3, 0, 0),
4443
Info = new OpenApiInfo
4544
{
4645

@@ -66,7 +65,6 @@ public void ParseBasicDocumentWithMultipleServersShouldSucceed()
6665

6766
openApiDoc.ShouldBeEquivalentTo(new OpenApiDocument()
6867
{
69-
SpecVersion = new Version(3,0,0),
7068
Info = new OpenApiInfo()
7169
{
7270
Title = "The API",
@@ -98,7 +96,6 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
9896

9997
openApiDoc.ShouldBeEquivalentTo(new OpenApiDocument()
10098
{
101-
SpecVersion = new Version(3, 0, 0),
10299
Info = new OpenApiInfo
103100
{
104101
Version = "0.9"
@@ -124,7 +121,6 @@ public void ParseMinimalDocumentShouldSucceed()
124121

125122
openApiDoc.ShouldBeEquivalentTo(new OpenApiDocument()
126123
{
127-
SpecVersion = new Version(3, 0, 0),
128124
Info = new OpenApiInfo
129125
{
130126

@@ -253,7 +249,6 @@ public void ParseStandardPetStoreDocumentShouldSucceed()
253249

254250
var expected = new OpenApiDocument()
255251
{
256-
SpecVersion = new Version(3, 0, 0),
257252
Info = new OpenApiInfo()
258253
{
259254
Version = "1.0.0",
@@ -711,7 +706,6 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
711706

712707
var expected = new OpenApiDocument()
713708
{
714-
SpecVersion = new Version(3, 0, 0),
715709
Info = new OpenApiInfo()
716710
{
717711
Version = "1.0.0",

0 commit comments

Comments
 (0)