From 091a575f92492acdeb445ef87b49fee024f5ef6c Mon Sep 17 00:00:00 2001 From: lixingzhi Date: Sat, 4 Mar 2023 16:38:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:openapi3=20=E6=96=87=E4=BB=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../openapi/AbstractOpenApiBuilder.java | 36 +++++++++---------- .../doc/builder/openapi/OpenApiBuilder.java | 30 +++++++++------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/power/doc/builder/openapi/AbstractOpenApiBuilder.java b/src/main/java/com/power/doc/builder/openapi/AbstractOpenApiBuilder.java index 38790d31..6948c401 100644 --- a/src/main/java/com/power/doc/builder/openapi/AbstractOpenApiBuilder.java +++ b/src/main/java/com/power/doc/builder/openapi/AbstractOpenApiBuilder.java @@ -282,27 +282,27 @@ public Map buildParametersSchema(ApiParam apiParam) { Map schema = new HashMap<>(10); String openApiType = DocUtil.javaTypeToOpenApiTypeConvert(apiParam.getType()); schema.put("type", openApiType); - if ("object".equals(openApiType) || "string".equals(openApiType)) { - if ("file".equals(apiParam.getType())) { - schema.put("format", "binary"); - schema.put("type", "string"); - } else if ("enum".equals(apiParam.getType())) { + if ("file".equals(apiParam.getType())) { + schema.put("format", "binary"); + schema.put("type", "string"); + } else if ("object".equals(openApiType)) { + if ("enum".equals(apiParam.getType())) { schema.put("enum", apiParam.getEnumValues()); - } else if (ARRAY.equals(apiParam.getType())) { - if (CollectionUtil.isNotEmpty(apiParam.getEnumValues())) { - schema.put("type", "string"); - schema.put("items", apiParam.getEnumValues()); - } else { - schema.put("type", ARRAY); - Map map = new HashMap<>(4); - map.put("type", "string"); - map.put("format", "string"); - schema.put("items", map); - } + } + } else if (ARRAY.equals(apiParam.getType())) { + if (CollectionUtil.isNotEmpty(apiParam.getEnumValues())) { + schema.put("type", "string"); + schema.put("items", apiParam.getEnumValues()); + } else { + schema.put("type", ARRAY); + Map map = new HashMap<>(4); + map.put("type", "string"); + map.put("format", "string"); + schema.put("items", map); } } else { - schema.put("type", "integer"); - schema.put("format", "int16".equals(apiParam.getType()) ? "int32" : apiParam.getType()); + schema.put("type", apiParam.getType()); + schema.put("format", "integer"); } return schema; } diff --git a/src/main/java/com/power/doc/builder/openapi/OpenApiBuilder.java b/src/main/java/com/power/doc/builder/openapi/OpenApiBuilder.java index b5ef9cbf..c32daf64 100644 --- a/src/main/java/com/power/doc/builder/openapi/OpenApiBuilder.java +++ b/src/main/java/com/power/doc/builder/openapi/OpenApiBuilder.java @@ -151,7 +151,7 @@ public Map buildPathUrlsRequest(ApiConfig apiConfig, ApiMethodDo request.put("parameters", buildParameters(apiMethodDoc)); request.put("responses", buildResponses(apiConfig, apiMethodDoc)); request.put("deprecated", apiMethodDoc.isDeprecated()); - String operationId = apiMethodDoc.getUrl().replace(apiMethodDoc.getServerUrl(),""); + String operationId = apiMethodDoc.getUrl().replace(apiMethodDoc.getServerUrl(), ""); request.put("operationId", String.join("", OpenApiSchemaUtil.getPatternResult("[A-Za-z0-9{}]*", operationId))); return request; @@ -239,26 +239,30 @@ List> buildParameters(ApiMethodDoc apiMethodDoc) { Map getStringParams(ApiParam apiParam, boolean hasItems) { Map parameters; parameters = new HashMap<>(20); - boolean isFile = "file".equalsIgnoreCase(apiParam.getType()); if (!hasItems) { parameters.put("name", apiParam.getField()); parameters.put("description", apiParam.getDesc()); parameters.put("required", apiParam.isRequired()); - parameters.put("example", StringUtil.removeQuotes(apiParam.getValue())); + parameters.put("in", "query"); + parameters.put("schema", buildParametersSchema(apiParam)); } else { - if (isFile) { - parameters.put("type", "string"); - parameters.put("format", "binary"); + if (OBJECT.equals(apiParam.getType()) || + (ARRAY.equals(apiParam.getType()) && apiParam.isHasItems())) { + parameters.put("type", "object"); + parameters.put("description", "(complex POJO please use @RequestBody)"); } else { - parameters.put("type", apiParam.getType()); + String desc = apiParam.getDesc(); + if (desc.contains(PARAM_TYPE_FILE)) { + parameters.put("type", PARAM_TYPE_FILE); + } else if (desc.contains("string")) { + parameters.put("type", "string"); + } else { + parameters.put("type", "integer"); + } } + parameters.putAll(buildParametersSchema(apiParam)); } - if (isFile) { - parameters.put("in", "formData"); - } else { - parameters.put("in", "query"); - } - parameters.put("schema", buildParametersSchema(apiParam)); + return parameters; }