From 8df2db0878a37750594b930806f748c07d100c27 Mon Sep 17 00:00:00 2001 From: Gimling Date: Mon, 9 Jan 2023 12:35:42 +0800 Subject: [PATCH] =?UTF-8?q?swagger=E5=AF=BC=E5=85=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=9D=9Eref=E7=9A=84=E5=B5=8C=E5=A5=97=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/torna/api/open/SwaggerApi.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/server/server-api/src/main/java/cn/torna/api/open/SwaggerApi.java b/server/server-api/src/main/java/cn/torna/api/open/SwaggerApi.java index 5888601b..09cd3415 100644 --- a/server/server-api/src/main/java/cn/torna/api/open/SwaggerApi.java +++ b/server/server-api/src/main/java/cn/torna/api/open/SwaggerApi.java @@ -459,6 +459,10 @@ public class SwaggerApi { return null; } JsonSchema jsonSchema = getJsonSchema($ref, openAPI); + return buildObjectParam(jsonSchema, openAPI, context); + } + + private static List buildObjectParam(JsonSchema jsonSchema, OpenAPI openAPI, BuildObjectParamContext context) { final Map properties = jsonSchema.getSchema().getProperties(); return Optional.ofNullable(properties) .orElse(Collections.emptyMap()).entrySet() @@ -475,12 +479,17 @@ public class SwaggerApi { .build(); String type = value.getType(); List children = null; - // 如果有子对象 + // 如果有子对象的ref if (value.get$ref() != null) { type = TYPE_OBJECT; final String child$ref = value.get$ref(); children = buildObjectParam(child$ref, openAPI, context); } + // 如果直接书写了子对象的内容 + if(value.getProperties()!=null){ + type = TYPE_OBJECT; + children = buildObjectParam(getJsonSchema(value), openAPI, context); + } // 如果是枚举字段 if (value.getEnum() != null) { type = TYPE_ENUM; @@ -499,6 +508,9 @@ public class SwaggerApi { String itemType = items.getType(); if (itemType != null) { type = "array[" + itemType + "]"; + if(TYPE_OBJECT.equals(itemType)){ + children = buildObjectParam(getJsonSchema(items), openAPI, context); + } } } param.setChildren(children); @@ -647,6 +659,10 @@ public class SwaggerApi { private static JsonSchema getJsonSchema(String $ref, OpenAPI openAPI) { String refName = getRefName($ref); Schema schema = openAPI.getComponents().getSchemas().get(refName); + return getJsonSchema(schema); + } + + private static JsonSchema getJsonSchema(Schema schema) { String type= null; try { type = schema.getType(); -- Gitee