diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs
index 164b3330b..3354a6717 100644
--- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs
+++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs
@@ -147,6 +147,35 @@ public OpenApiDocument(OpenApiDocument? document)
BaseUri = document?.BaseUri != null ? document.BaseUri : new(OpenApiConstants.BaseRegistryUri + Guid.NewGuid());
}
+ ///
+ /// Serialize to an Open API document using the specified version.
+ ///
+ /// The Open API specification version to serialize the document as.
+ /// The to serialize the document to.
+ ///
+ /// is not a supported Open API specification version.
+ ///
+ public void SerializeAs(OpenApiSpecVersion version, IOpenApiWriter writer)
+ {
+ switch (version)
+ {
+ case OpenApiSpecVersion.OpenApi2_0:
+ SerializeAsV2(writer);
+ break;
+
+ case OpenApiSpecVersion.OpenApi3_0:
+ SerializeAsV3(writer);
+ break;
+
+ case OpenApiSpecVersion.OpenApi3_1:
+ SerializeAsV31(writer);
+ break;
+
+ default:
+ throw new ArgumentOutOfRangeException(nameof(version), version, string.Format(Properties.SRResource.OpenApiSpecVersionNotSupported, version));
+ }
+ }
+
///
/// Serialize to Open API v3.1 document.
///
diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsVersionJsonWorksAsync_version=OpenApi2_0.verified.txt b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsVersionJsonWorksAsync_version=OpenApi2_0.verified.txt
new file mode 100644
index 000000000..09804c666
--- /dev/null
+++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsVersionJsonWorksAsync_version=OpenApi2_0.verified.txt
@@ -0,0 +1,417 @@
+{
+ "swagger": "2.0",
+ "info": {
+ "title": "Swagger Petstore (Simple)",
+ "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
+ "termsOfService": "http://helloreverb.com/terms/",
+ "contact": {
+ "name": "Swagger API team",
+ "url": "http://swagger.io",
+ "email": "foo@example.com"
+ },
+ "license": {
+ "name": "MIT",
+ "url": "http://opensource.org/licenses/MIT"
+ },
+ "version": "1.0.0"
+ },
+ "host": "petstore.swagger.io",
+ "basePath": "/api",
+ "schemes": [
+ "http"
+ ],
+ "paths": {
+ "/pets": {
+ "get": {
+ "description": "Returns all pets from the system that the user has access to",
+ "operationId": "findPets",
+ "produces": [
+ "application/json",
+ "application/xml",
+ "text/html"
+ ],
+ "parameters": [
+ {
+ "in": "query",
+ "name": "tags",
+ "description": "tags to filter by",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "collectionFormat": "multi"
+ },
+ {
+ "in": "query",
+ "name": "limit",
+ "description": "maximum number of results to return",
+ "type": "integer",
+ "format": "int32"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "pet response",
+ "schema": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "required": [
+ "id",
+ "name"
+ ],
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "schema": {
+ "type": "object",
+ "required": [
+ "code",
+ "message"
+ ],
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "schema": {
+ "type": "object",
+ "required": [
+ "code",
+ "message"
+ ],
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "post": {
+ "description": "Creates a new pet in the store. Duplicates are allowed",
+ "operationId": "addPet",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json",
+ "text/html"
+ ],
+ "parameters": [
+ {
+ "in": "body",
+ "name": "body",
+ "description": "Pet to add to the store",
+ "required": true,
+ "schema": {
+ "type": "object",
+ "required": [
+ "name"
+ ],
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "pet response",
+ "schema": {
+ "type": "object",
+ "required": [
+ "id",
+ "name"
+ ],
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "schema": {
+ "type": "object",
+ "required": [
+ "code",
+ "message"
+ ],
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "schema": {
+ "type": "object",
+ "required": [
+ "code",
+ "message"
+ ],
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "/pets/{id}": {
+ "get": {
+ "description": "Returns a user based on a single ID, if the user does not have access to the pet",
+ "operationId": "findPetById",
+ "produces": [
+ "application/json",
+ "application/xml",
+ "text/html"
+ ],
+ "parameters": [
+ {
+ "in": "path",
+ "name": "id",
+ "description": "ID of pet to fetch",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "pet response",
+ "schema": {
+ "type": "object",
+ "required": [
+ "id",
+ "name"
+ ],
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "schema": {
+ "type": "object",
+ "required": [
+ "code",
+ "message"
+ ],
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "schema": {
+ "type": "object",
+ "required": [
+ "code",
+ "message"
+ ],
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "delete": {
+ "description": "deletes a single pet based on the ID supplied",
+ "operationId": "deletePet",
+ "produces": [
+ "text/html"
+ ],
+ "parameters": [
+ {
+ "in": "path",
+ "name": "id",
+ "description": "ID of pet to delete",
+ "required": true,
+ "type": "integer",
+ "format": "int64"
+ }
+ ],
+ "responses": {
+ "204": {
+ "description": "pet deleted"
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "schema": {
+ "type": "object",
+ "required": [
+ "code",
+ "message"
+ ],
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "schema": {
+ "type": "object",
+ "required": [
+ "code",
+ "message"
+ ],
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "definitions": {
+ "pet": {
+ "type": "object",
+ "required": [
+ "id",
+ "name"
+ ],
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ },
+ "newPet": {
+ "type": "object",
+ "required": [
+ "name"
+ ],
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ },
+ "errorModel": {
+ "type": "object",
+ "required": [
+ "code",
+ "message"
+ ],
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsVersionJsonWorksAsync_version=OpenApi3_0.verified.txt b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsVersionJsonWorksAsync_version=OpenApi3_0.verified.txt
new file mode 100644
index 000000000..5d9d7f3da
--- /dev/null
+++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsVersionJsonWorksAsync_version=OpenApi3_0.verified.txt
@@ -0,0 +1,495 @@
+{
+ "openapi": "3.0.4",
+ "info": {
+ "title": "Swagger Petstore (Simple)",
+ "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
+ "termsOfService": "http://helloreverb.com/terms/",
+ "contact": {
+ "name": "Swagger API team",
+ "url": "http://swagger.io",
+ "email": "foo@example.com"
+ },
+ "license": {
+ "name": "MIT",
+ "url": "http://opensource.org/licenses/MIT"
+ },
+ "version": "1.0.0"
+ },
+ "servers": [
+ {
+ "url": "http://petstore.swagger.io/api"
+ }
+ ],
+ "paths": {
+ "/pets": {
+ "get": {
+ "description": "Returns all pets from the system that the user has access to",
+ "operationId": "findPets",
+ "parameters": [
+ {
+ "name": "tags",
+ "in": "query",
+ "description": "tags to filter by",
+ "schema": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ },
+ {
+ "name": "limit",
+ "in": "query",
+ "description": "maximum number of results to return",
+ "schema": {
+ "type": "integer",
+ "format": "int32"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "pet response",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "array",
+ "items": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ },
+ "application/xml": {
+ "schema": {
+ "type": "array",
+ "items": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "post": {
+ "description": "Creates a new pet in the store. Duplicates are allowed",
+ "operationId": "addPet",
+ "requestBody": {
+ "description": "Pet to add to the store",
+ "content": {
+ "application/json": {
+ "schema": {
+ "required": [
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "200": {
+ "description": "pet response",
+ "content": {
+ "application/json": {
+ "schema": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "/pets/{id}": {
+ "get": {
+ "description": "Returns a user based on a single ID, if the user does not have access to the pet",
+ "operationId": "findPetById",
+ "parameters": [
+ {
+ "name": "id",
+ "in": "path",
+ "description": "ID of pet to fetch",
+ "required": true,
+ "schema": {
+ "type": "integer",
+ "format": "int64"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "pet response",
+ "content": {
+ "application/json": {
+ "schema": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "application/xml": {
+ "schema": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "delete": {
+ "description": "deletes a single pet based on the ID supplied",
+ "operationId": "deletePet",
+ "parameters": [
+ {
+ "name": "id",
+ "in": "path",
+ "description": "ID of pet to delete",
+ "required": true,
+ "schema": {
+ "type": "integer",
+ "format": "int64"
+ }
+ }
+ ],
+ "responses": {
+ "204": {
+ "description": "pet deleted"
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "components": {
+ "schemas": {
+ "pet": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ },
+ "newPet": {
+ "required": [
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ },
+ "errorModel": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsVersionJsonWorksAsync_version=OpenApi3_1.verified.txt b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsVersionJsonWorksAsync_version=OpenApi3_1.verified.txt
new file mode 100644
index 000000000..3c9768fe9
--- /dev/null
+++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsVersionJsonWorksAsync_version=OpenApi3_1.verified.txt
@@ -0,0 +1,495 @@
+{
+ "openapi": "3.1.1",
+ "info": {
+ "title": "Swagger Petstore (Simple)",
+ "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
+ "termsOfService": "http://helloreverb.com/terms/",
+ "contact": {
+ "name": "Swagger API team",
+ "url": "http://swagger.io",
+ "email": "foo@example.com"
+ },
+ "license": {
+ "name": "MIT",
+ "url": "http://opensource.org/licenses/MIT"
+ },
+ "version": "1.0.0"
+ },
+ "servers": [
+ {
+ "url": "http://petstore.swagger.io/api"
+ }
+ ],
+ "paths": {
+ "/pets": {
+ "get": {
+ "description": "Returns all pets from the system that the user has access to",
+ "operationId": "findPets",
+ "parameters": [
+ {
+ "name": "tags",
+ "in": "query",
+ "description": "tags to filter by",
+ "schema": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ },
+ {
+ "name": "limit",
+ "in": "query",
+ "description": "maximum number of results to return",
+ "schema": {
+ "type": "integer",
+ "format": "int32"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "pet response",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "array",
+ "items": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ },
+ "application/xml": {
+ "schema": {
+ "type": "array",
+ "items": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "post": {
+ "description": "Creates a new pet in the store. Duplicates are allowed",
+ "operationId": "addPet",
+ "requestBody": {
+ "description": "Pet to add to the store",
+ "content": {
+ "application/json": {
+ "schema": {
+ "required": [
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "200": {
+ "description": "pet response",
+ "content": {
+ "application/json": {
+ "schema": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "/pets/{id}": {
+ "get": {
+ "description": "Returns a user based on a single ID, if the user does not have access to the pet",
+ "operationId": "findPetById",
+ "parameters": [
+ {
+ "name": "id",
+ "in": "path",
+ "description": "ID of pet to fetch",
+ "required": true,
+ "schema": {
+ "type": "integer",
+ "format": "int64"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "pet response",
+ "content": {
+ "application/json": {
+ "schema": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "application/xml": {
+ "schema": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "delete": {
+ "description": "deletes a single pet based on the ID supplied",
+ "operationId": "deletePet",
+ "parameters": [
+ {
+ "name": "id",
+ "in": "path",
+ "description": "ID of pet to delete",
+ "required": true,
+ "schema": {
+ "type": "integer",
+ "format": "int64"
+ }
+ }
+ ],
+ "responses": {
+ "204": {
+ "description": "pet deleted"
+ },
+ "4XX": {
+ "description": "unexpected client error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ },
+ "5XX": {
+ "description": "unexpected server error",
+ "content": {
+ "text/html": {
+ "schema": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "components": {
+ "schemas": {
+ "pet": {
+ "required": [
+ "id",
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ },
+ "newPet": {
+ "required": [
+ "name"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "format": "int64"
+ },
+ "name": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ }
+ },
+ "errorModel": {
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "format": "int32"
+ },
+ "message": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
index 76102456e..0b7e25f9b 100644
--- a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
+++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
@@ -2135,5 +2135,49 @@ public void DeduplicatesTags()
Assert.Contains(document.Tags, t => t.Name == "tag1");
Assert.Contains(document.Tags, t => t.Name == "tag2");
}
+
+ public static TheoryData OpenApiSpecVersions()
+ {
+ var values = new TheoryData();
+
+ foreach (var value in Enum.GetValues())
+ {
+ values.Add(value);
+ }
+
+ return values;
+ }
+
+ [Theory]
+ [MemberData(nameof(OpenApiSpecVersions))]
+ public async Task SerializeAdvancedDocumentAsVersionJsonWorksAsync(OpenApiSpecVersion version)
+ {
+ // Arrange
+ using var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
+ var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = false });
+
+ // Act
+ AdvancedDocument.SerializeAs(version, writer);
+ await writer.FlushAsync();
+
+ // Assert
+ await Verifier.Verify(outputStringWriter).UseParameters(version);
+ }
+
+ [Fact]
+ public void SerializeAsThrowsIfVersionIsNotSupported()
+ {
+ // Arrange
+ using var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
+ var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = false });
+ var version = (OpenApiSpecVersion)int.MaxValue;
+
+ // Act
+ var actual = Assert.Throws(() => AdvancedDocument.SerializeAs(version, writer));
+
+ // Assert
+ Assert.Equal("version", actual.ParamName);
+ Assert.Equal(version, actual.ActualValue);
+ }
}
}
diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt
index 13fdf06fe..2e672dde0 100644
--- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt
+++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt
@@ -727,6 +727,7 @@ namespace Microsoft.OpenApi.Models
public bool AddComponent(string id, T componentToRegister) { }
public System.Threading.Tasks.Task GetHashCodeAsync(System.Threading.CancellationToken cancellationToken = default) { }
public void RegisterComponents() { }
+ public void SerializeAs(Microsoft.OpenApi.OpenApiSpecVersion version, Microsoft.OpenApi.Writers.IOpenApiWriter writer) { }
public void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { }
public void SerializeAsV3(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { }
public void SerializeAsV31(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { }