Skip to content

Commit 371a574

Browse files
authored
Merge pull request #2253 from martincostello/gh-2231
feat: adds OpenApiDocument.SerializeAs() so simplify serialization scenarios
2 parents 8baff28 + 4084e40 commit 371a574

6 files changed

+1481
-0
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

+29
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,35 @@ public OpenApiDocument(OpenApiDocument? document)
147147
BaseUri = document?.BaseUri != null ? document.BaseUri : new(OpenApiConstants.BaseRegistryUri + Guid.NewGuid());
148148
}
149149

150+
/// <summary>
151+
/// Serialize <see cref="OpenApiDocument"/> to an Open API document using the specified version.
152+
/// </summary>
153+
/// <param name="version">The Open API specification version to serialize the document as.</param>
154+
/// <param name="writer">The <see cref="IOpenApiWriter"/> to serialize the document to.</param>
155+
/// <exception cref="ArgumentOutOfRangeException">
156+
/// <paramref name="version"/> is not a supported Open API specification version.
157+
/// </exception>
158+
public void SerializeAs(OpenApiSpecVersion version, IOpenApiWriter writer)
159+
{
160+
switch (version)
161+
{
162+
case OpenApiSpecVersion.OpenApi2_0:
163+
SerializeAsV2(writer);
164+
break;
165+
166+
case OpenApiSpecVersion.OpenApi3_0:
167+
SerializeAsV3(writer);
168+
break;
169+
170+
case OpenApiSpecVersion.OpenApi3_1:
171+
SerializeAsV31(writer);
172+
break;
173+
174+
default:
175+
throw new ArgumentOutOfRangeException(nameof(version), version, string.Format(Properties.SRResource.OpenApiSpecVersionNotSupported, version));
176+
}
177+
}
178+
150179
/// <summary>
151180
/// Serialize <see cref="OpenApiDocument"/> to Open API v3.1 document.
152181
/// </summary>

0 commit comments

Comments
 (0)