Skip to content

Commit d57aad8

Browse files
committed
Implement SerializeAs31 across model objects
1 parent 00e19eb commit d57aad8

31 files changed

+620
-276
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static void Serialize<T>(this T element, IOpenApiWriter writer, OpenApiSp
118118
switch (specVersion)
119119
{
120120
case OpenApiSpecVersion.OpenApi3_1:
121-
element.SerializeAsV3(writer, OpenApiSpecVersion.OpenApi3_1);
121+
element.SerializeAsV31(writer);
122122
break;
123123

124124
case OpenApiSpecVersion.OpenApi3_0:

src/Microsoft.OpenApi/Interfaces/IOpenApiSerializable.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ namespace Microsoft.OpenApi.Interfaces
1010
/// </summary>
1111
public interface IOpenApiSerializable : IOpenApiElement
1212
{
13+
/// <summary>
14+
/// Serialize OpenAPI element into v3.1
15+
/// </summary>
16+
/// <param name="writer"></param>
17+
void SerializeAsV31(IOpenApiWriter writer);
18+
1319
/// <summary>
1420
/// Serialize Open API element to v3.0.
1521
/// </summary>
1622
/// <param name="writer">The writer.</param>
17-
/// <param name="specVersion">The OpenApi specification version.</param>
18-
void SerializeAsV3(IOpenApiWriter writer, OpenApiSpecVersion specVersion = OpenApiSpecVersion.OpenApi3_0);
23+
void SerializeAsV3(IOpenApiWriter writer);
1924

2025
/// <summary>
2126
/// Serialize Open API element to v2.0.

src/Microsoft.OpenApi/Models/OpenApiCallback.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,32 @@ public void AddPathItem(RuntimeExpression expression, OpenApiPathItem pathItem)
7575

7676
PathItems.Add(expression, pathItem);
7777
}
78-
78+
79+
/// <summary>
80+
/// Serialize <see cref="OpenApiCallback"/> to Open Api v3.1
81+
/// </summary>
82+
/// <param name="writer"></param>
83+
/// <exception cref="System.NotImplementedException"></exception>
84+
public void SerializeAsV31(IOpenApiWriter writer)
85+
{
86+
Serialize(writer);
87+
}
88+
7989
/// <summary>
8090
/// Serialize <see cref="OpenApiCallback"/> to Open Api v3.0
8191
/// </summary>
82-
public void SerializeAsV3(IOpenApiWriter writer, OpenApiSpecVersion version = OpenApiSpecVersion.OpenApi3_0)
92+
public void SerializeAsV3(IOpenApiWriter writer)
8393
{
84-
if (writer == null)
85-
{
86-
throw Error.ArgumentNull(nameof(writer));
87-
}
94+
Serialize(writer);
95+
}
96+
97+
/// <summary>
98+
/// Serialize <see cref="OpenApiCallback"/>
99+
/// </summary>
100+
/// <param name="writer"></param>
101+
public void Serialize(IOpenApiWriter writer)
102+
{
103+
writer = writer ?? throw Error.ArgumentNull(nameof(writer));
88104

89105
var target = this;
90106

src/Microsoft.OpenApi/Models/OpenApiComponents.cs

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using Microsoft.OpenApi.Interfaces;
@@ -95,14 +96,50 @@ public OpenApiComponents(OpenApiComponents components)
9596
}
9697

9798
/// <summary>
98-
/// Serialize <see cref="OpenApiComponents"/> to Open Api v3.0.
99+
/// Serialize <see cref="OpenApiComponents"/> to Open API v3.1.
99100
/// </summary>
100-
public void SerializeAsV3(IOpenApiWriter writer, OpenApiSpecVersion version = OpenApiSpecVersion.OpenApi3_0)
101-
{
102-
if (writer == null)
101+
/// <param name="writer"></param>
102+
public void SerializeAsV31(IOpenApiWriter writer)
103+
{
104+
Serialize(writer);
105+
106+
// pathItems - only present in v3.1
107+
writer.WriteOptionalMap(
108+
OpenApiConstants.PathItems,
109+
PathItems,
110+
(w, key, component) =>
103111
{
104-
throw Error.ArgumentNull(nameof(writer));
105-
}
112+
if (component.Reference != null &&
113+
component.Reference.Type == ReferenceType.Schema &&
114+
component.Reference.Id == key)
115+
{
116+
component.SerializeAsV3WithoutReference(w);
117+
}
118+
else
119+
{
120+
component.SerializeAsV3(w);
121+
}
122+
});
123+
124+
writer.WriteEndObject();
125+
}
126+
127+
/// <summary>
128+
/// Serialize <see cref="OpenApiComponents"/> to v3.0
129+
/// </summary>
130+
/// <param name="writer"></param>
131+
public void SerializeAsV3(IOpenApiWriter writer)
132+
{
133+
Serialize(writer);
134+
writer.WriteEndObject();
135+
}
136+
137+
/// <summary>
138+
/// Serialize <see cref="OpenApiComponents"/>.
139+
/// </summary>
140+
public void Serialize(IOpenApiWriter writer)
141+
{
142+
writer = writer ?? throw Error.ArgumentNull(nameof(writer));
106143

107144
// If references have been inlined we don't need the to render the components section
108145
// however if they have cycles, then we will need a component rendered
@@ -293,31 +330,8 @@ public void SerializeAsV3(IOpenApiWriter writer, OpenApiSpecVersion version = Op
293330
}
294331
});
295332

296-
// pathItems - only present in v3.1
297-
if(version == OpenApiSpecVersion.OpenApi3_1)
298-
{
299-
writer.WriteOptionalMap(
300-
OpenApiConstants.PathItems,
301-
PathItems,
302-
(w, key, component) =>
303-
{
304-
if (component.Reference != null &&
305-
component.Reference.Type == ReferenceType.Schema &&
306-
component.Reference.Id == key)
307-
{
308-
component.SerializeAsV3WithoutReference(w);
309-
}
310-
else
311-
{
312-
component.SerializeAsV3(w);
313-
}
314-
});
315-
}
316-
317333
// extensions
318-
writer.WriteExtensions(Extensions, version);
319-
320-
writer.WriteEndObject();
334+
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi3_0);
321335
}
322336

323337
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiContact.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,22 @@ public OpenApiContact(OpenApiContact contact)
5252
}
5353

5454
/// <summary>
55-
/// Serialize <see cref="OpenApiContact"/> to Open Api v3.0
55+
/// Serialize <see cref="OpenApiContact"/> to Open Api v3.1
5656
/// </summary>
57-
public void SerializeAsV3(IOpenApiWriter writer, OpenApiSpecVersion version = OpenApiSpecVersion.OpenApi3_0)
57+
/// <param name="writer"></param>
58+
public void SerializeAsV31(IOpenApiWriter writer)
5859
{
59-
WriteInternal(writer, version);
60+
WriteInternal(writer, OpenApiSpecVersion.OpenApi3_1);
6061
}
6162

63+
/// <summary>
64+
/// Serialize <see cref="OpenApiContact"/> to Open Api v3.0
65+
/// </summary>
66+
public void SerializeAsV3(IOpenApiWriter writer)
67+
{
68+
WriteInternal(writer, OpenApiSpecVersion.OpenApi3_0);
69+
}
70+
6271
/// <summary>
6372
/// Serialize <see cref="OpenApiContact"/> to Open Api v2.0
6473
/// </summary>
@@ -69,10 +78,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
6978

7079
private void WriteInternal(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
7180
{
72-
if (writer == null)
73-
{
74-
throw Error.ArgumentNull(nameof(writer));
75-
}
81+
writer = writer ?? throw Error.ArgumentNull(nameof(writer));
7682

7783
writer.WriteStartObject();
7884

src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,30 @@ public OpenApiDiscriminator(OpenApiDiscriminator discriminator)
3636
Mapping = discriminator?.Mapping != null ? new Dictionary<string, string>(discriminator.Mapping) : null;
3737
}
3838

39+
/// <summary>
40+
/// Serialize <see cref="OpenApiDiscriminator"/> to Open Api v3.1
41+
/// </summary>
42+
/// <param name="writer"></param>
43+
public void SerializeAsV31(IOpenApiWriter writer)
44+
{
45+
Serialize(writer);
46+
}
47+
48+
/// <summary>
49+
/// Serialize <see cref="OpenApiDiscriminator"/> to Open Api v3.0
50+
/// </summary>
51+
public void SerializeAsV3(IOpenApiWriter writer)
52+
{
53+
Serialize(writer);
54+
}
55+
3956
/// <summary>
4057
/// Serialize <see cref="OpenApiDiscriminator"/> to Open Api v3.0
4158
/// </summary>
42-
public void SerializeAsV3(IOpenApiWriter writer, OpenApiSpecVersion version = OpenApiSpecVersion.OpenApi3_0)
59+
/// <param name="writer"></param>
60+
public void Serialize(IOpenApiWriter writer)
4361
{
44-
if (writer == null)
45-
{
46-
throw Error.ArgumentNull(nameof(writer));
47-
}
62+
writer = writer ?? throw Error.ArgumentNull(nameof(writer));
4863

4964
writer.WriteStartObject();
5065

@@ -53,8 +68,6 @@ public void SerializeAsV3(IOpenApiWriter writer, OpenApiSpecVersion version = Op
5368

5469
// mapping
5570
writer.WriteOptionalMap(OpenApiConstants.Mapping, Mapping, (w, s) => w.WriteValue(s));
56-
57-
writer.WriteEndObject();
5871
}
5972

6073
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -106,65 +106,75 @@ public OpenApiDocument(OpenApiDocument document)
106106
}
107107

108108
/// <summary>
109-
/// Serialize <see cref="OpenApiDocument"/> to the latest patch of OpenAPI object V3.0.
109+
/// Serialize <see cref="OpenApiDocument"/> to Open API v3.1 document.
110110
/// </summary>
111-
public void SerializeAsV3(IOpenApiWriter writer, OpenApiSpecVersion version = OpenApiSpecVersion.OpenApi3_0)
111+
/// <param name="writer"></param>
112+
public void SerializeAsV31(IOpenApiWriter writer)
112113
{
113-
if (writer == null)
114-
{
115-
throw Error.ArgumentNull(nameof(writer));
116-
}
114+
writer = writer ?? throw Error.ArgumentNull(nameof(writer));
117115

118116
writer.WriteStartObject();
117+
118+
// openApi;
119+
writer.WriteProperty(OpenApiConstants.OpenApi, "3.1.0");
120+
121+
// jsonSchemaDialect
122+
writer.WriteProperty(OpenApiConstants.JsonSchemaDialect, JsonSchemaDialect);
119123

120-
// openapi
121-
switch (version)
124+
Serialize(writer);
125+
126+
// webhooks
127+
writer.WriteOptionalMap(
128+
OpenApiConstants.Webhooks,
129+
Webhooks,
130+
(w, key, component) =>
122131
{
123-
case OpenApiSpecVersion.OpenApi3_1:
124-
writer.WriteProperty(OpenApiConstants.OpenApi, "3.1.0");
125-
break;
126-
case OpenApiSpecVersion.OpenApi3_0:
127-
writer.WriteProperty(OpenApiConstants.OpenApi, "3.0.1");
128-
break;
129-
default:
130-
writer.WriteProperty(OpenApiConstants.OpenApi, "3.0.1");
131-
break;
132-
}
132+
if (component.Reference != null &&
133+
component.Reference.Type == ReferenceType.PathItem &&
134+
component.Reference.Id == key)
135+
{
136+
component.SerializeAsV3WithoutReference(w);
137+
}
138+
else
139+
{
140+
component.SerializeAsV31(w);
141+
}
142+
});
143+
144+
writer.WriteEndObject();
145+
}
146+
147+
/// <summary>
148+
/// Serialize <see cref="OpenApiDocument"/> to the latest patch of OpenAPI object V3.0.
149+
/// </summary>
150+
public void SerializeAsV3(IOpenApiWriter writer)
151+
{
152+
153+
writer = writer ?? throw Error.ArgumentNull(nameof(writer));
154+
155+
writer.WriteStartObject();
133156

157+
// openapi
158+
writer.WriteProperty(OpenApiConstants.OpenApi, "3.0.1");
159+
Serialize(writer);
160+
writer.WriteEndObject();
161+
}
162+
163+
/// <summary>
164+
/// Serialize <see cref="OpenApiDocument"/>
165+
/// </summary>
166+
/// <param name="writer"></param>
167+
public void Serialize(IOpenApiWriter writer)
168+
{
134169
// info
135170
writer.WriteRequiredObject(OpenApiConstants.Info, Info, (w, i) => i.SerializeAsV3(w));
136171

137-
// jsonSchemaDialect
138-
if(version == OpenApiSpecVersion.OpenApi3_1)
139-
writer.WriteProperty(OpenApiConstants.JsonSchemaDialect, JsonSchemaDialect);
140-
141172
// servers
142173
writer.WriteOptionalCollection(OpenApiConstants.Servers, Servers, (w, s) => s.SerializeAsV3(w));
143174

144175
// paths
145176
writer.WriteRequiredObject(OpenApiConstants.Paths, Paths, (w, p) => p.SerializeAsV3(w));
146177

147-
// webhooks
148-
if (version == OpenApiSpecVersion.OpenApi3_1)
149-
{
150-
writer.WriteOptionalMap(
151-
OpenApiConstants.Webhooks,
152-
Webhooks,
153-
(w, key, component) =>
154-
{
155-
if (component.Reference != null &&
156-
component.Reference.Type == ReferenceType.PathItem &&
157-
component.Reference.Id == key)
158-
{
159-
component.SerializeAsV3WithoutReference(w);
160-
}
161-
else
162-
{
163-
component.SerializeAsV3(w);
164-
}
165-
});
166-
}
167-
168178
// components
169179
writer.WriteOptionalObject(OpenApiConstants.Components, Components, (w, c) => c.SerializeAsV3(w));
170180

@@ -181,9 +191,7 @@ public void SerializeAsV3(IOpenApiWriter writer, OpenApiSpecVersion version = Op
181191
writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, e) => e.SerializeAsV3(w));
182192

183193
// extensions
184-
writer.WriteExtensions(Extensions, version);
185-
186-
writer.WriteEndObject();
194+
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi3_0);
187195
}
188196

189197
/// <summary>

0 commit comments

Comments
 (0)