我发现OpenAPIV3Parser
提供了对Open API doc JSON进行反序列化的方法,但不允许将其序列化回去。例如,如果我需要制作SwaggerParseResult
的深层副本,我无法将其序列化回JSON,然后通过反序列化该JSON来创建新的SwaggerParseResult
。事实上,我不确定从SwaggerParseResult
恢复原始JSON的官方指导方针是什么(假设我没有将其存储在内存中)
请注意,重新序列化的JSON应该是有效的:它应该能够重新序列化回SwaggerParseResult
,在swaggerParseResult.getOpenAPI()
调用时返回有效的(非空值,Swagger UI接受)OpenAPI
是否有任何变通办法?
下面是我尝试过的:
package com.example.dynamicgateway.parser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.junit.jupiter.api.Test;
public class ParserTest {
private final OpenAPIV3Parser parser = new OpenAPIV3Parser();
@Test
void testParser() throws JsonProcessingException {
String testDoc = getTestDoc();
SwaggerParseResult parseResult = parser.readContents(testDoc);
// I tried with and without writerWithDefaultPrettyPrinter()
String reserializedDoc = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(parseResult);
// doesn't work, OpenApi is null
SwaggerParseResult redeserializedDoc = parser.readContents(reserializedDoc);
}
String getTestDoc() {
return """
{
"openapi": "3.0.1",
"info": {
"title": "Hello World API",
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:8090",
"description": "Generated server url"
}
],
"paths": {
"/joy": {
"get": {
"tags": [
"Message Controller"
],
"operationId": "getMessageOfJoy",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/SuccessMessage"
}
}
}
}
}
}
},
"/auth/hello-world": {
"get": {
"tags": [
"Message Controller"
],
"operationId": "getHelloWorld",
"parameters": [
{
"name": "principal",
"in": "query",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/SuccessMessage"
}
}
}
}
}
}
},
"/error": {
"get": {
"tags": [
"my-error-controller"
],
"operationId": "error",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/FailureMessage"
}
}
}
}
}
},
"put": {
"tags": [
"my-error-controller"
],
"operationId": "error_3",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/FailureMessage"
}
}
}
}
}
},
"post": {
"tags": [
"my-error-controller"
],
"operationId": "error_2",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/FailureMessage"
}
}
}
}
}
},
"delete": {
"tags": [
"my-error-controller"
],
"operationId": "error_5",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/FailureMessage"
}
}
}
}
}
},
"options": {
"tags": [
"my-error-controller"
],
"operationId": "error_6",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/FailureMessage"
}
}
}
}
}
},
"head": {
"tags": [
"my-error-controller"
],
"operationId": "error_1",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/FailureMessage"
}
}
}
}
}
},
"patch": {
"tags": [
"my-error-controller"
],
"operationId": "error_4",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/FailureMessage"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"SuccessMessage": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"FailureMessage": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"method": {
"type": "string",
"enum": [
"GET",
"HEAD",
"POST",
"PUT",
"PATCH",
"DELETE",
"OPTIONS",
"TRACE"
]
},
"request_path": {
"type": "string"
}
}
}
}
}
}
""";
}
}
字符串
1条答案
按热度按时间b1uwtaje1#
你想做一个深拷贝?好吧。下面是你如何做一个深拷贝:
字符串
您可以将此技术应用于任何东西,无论是否开箱即用