From a35d4c63d58ab0da638263954c047e58bc6950bf Mon Sep 17 00:00:00 2001 From: caiti <caiti@syiti.com> Date: Fri, 11 Nov 2022 18:29:35 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=907.3.2=E3=80=91=E6=96=B0=E5=A2=9Eopenap?= =?UTF-8?q?i=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel-d-openapi/README.md | 1 + kernel-d-openapi/openapi-api/pom.xml | 35 ++++++ .../api/constants/OpenApiConstants.java | 38 +++++++ .../api/exception/OpenApiException.java | 43 +++++++ .../openapi-sdk-springdoc/pom.xml | 34 ++++++ .../converter/GunsModelConverterImpl.java | 80 +++++++++++++ .../providers/GunsJavadocProviderImpl.java | 107 ++++++++++++++++++ .../openapi-spring-boot-starter/pom.xml | 29 +++++ .../SpringdocOpenApiAutoConfiguration.java | 56 +++++++++ .../main/resources/META-INF/spring.factories | 2 + kernel-d-openapi/pom.xml | 23 ++++ pom.xml | 9 ++ 12 files changed, 457 insertions(+) create mode 100644 kernel-d-openapi/README.md create mode 100644 kernel-d-openapi/openapi-api/pom.xml create mode 100644 kernel-d-openapi/openapi-api/src/main/java/cn/stylefeng/roses/kernel/openapi/api/constants/OpenApiConstants.java create mode 100644 kernel-d-openapi/openapi-api/src/main/java/cn/stylefeng/roses/kernel/openapi/api/exception/OpenApiException.java create mode 100644 kernel-d-openapi/openapi-sdk-springdoc/pom.xml create mode 100644 kernel-d-openapi/openapi-sdk-springdoc/src/main/java/cn/stylefeng/roses/kernel/springdoc/converter/GunsModelConverterImpl.java create mode 100644 kernel-d-openapi/openapi-sdk-springdoc/src/main/java/cn/stylefeng/roses/kernel/springdoc/providers/GunsJavadocProviderImpl.java create mode 100644 kernel-d-openapi/openapi-spring-boot-starter/pom.xml create mode 100644 kernel-d-openapi/openapi-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/springdoc/starter/SpringdocOpenApiAutoConfiguration.java create mode 100644 kernel-d-openapi/openapi-spring-boot-starter/src/main/resources/META-INF/spring.factories create mode 100644 kernel-d-openapi/pom.xml diff --git a/kernel-d-openapi/README.md b/kernel-d-openapi/README.md new file mode 100644 index 000000000..d3670146d --- /dev/null +++ b/kernel-d-openapi/README.md @@ -0,0 +1 @@ +openapi接口文档适配 diff --git a/kernel-d-openapi/openapi-api/pom.xml b/kernel-d-openapi/openapi-api/pom.xml new file mode 100644 index 000000000..20104200c --- /dev/null +++ b/kernel-d-openapi/openapi-api/pom.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>kernel-d-openapi</artifactId> + <groupId>cn.stylefeng.roses</groupId> + <version>7.3.2</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>openapi-api</artifactId> + + <packaging>jar</packaging> + + <dependencies> + + <!-- 开发规则 --> + <dependency> + <groupId>cn.stylefeng.roses</groupId> + <artifactId>kernel-a-rule</artifactId> + <version>${roses.version}</version> + </dependency> + + <!--资源模块的api--> + <dependency> + <groupId>cn.stylefeng.roses</groupId> + <artifactId>scanner-api</artifactId> + <version>${roses.version}</version> + </dependency> + + </dependencies> + +</project> \ No newline at end of file diff --git a/kernel-d-openapi/openapi-api/src/main/java/cn/stylefeng/roses/kernel/openapi/api/constants/OpenApiConstants.java b/kernel-d-openapi/openapi-api/src/main/java/cn/stylefeng/roses/kernel/openapi/api/constants/OpenApiConstants.java new file mode 100644 index 000000000..2fb65dff0 --- /dev/null +++ b/kernel-d-openapi/openapi-api/src/main/java/cn/stylefeng/roses/kernel/openapi/api/constants/OpenApiConstants.java @@ -0,0 +1,38 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.openapi.api.constants; + +/** + * OpenApi模块的常量 + * + * @author caiti + * @date 2022-11-11 + */ +public interface OpenApiConstants { + /** + * OpenApi模块的名称 + */ + String OPENAPI_MODULE_NAME = "kernel-d-openapi"; +} diff --git a/kernel-d-openapi/openapi-api/src/main/java/cn/stylefeng/roses/kernel/openapi/api/exception/OpenApiException.java b/kernel-d-openapi/openapi-api/src/main/java/cn/stylefeng/roses/kernel/openapi/api/exception/OpenApiException.java new file mode 100644 index 000000000..ff4976136 --- /dev/null +++ b/kernel-d-openapi/openapi-api/src/main/java/cn/stylefeng/roses/kernel/openapi/api/exception/OpenApiException.java @@ -0,0 +1,43 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.openapi.api.exception; + +import cn.stylefeng.roses.kernel.openapi.api.constants.OpenApiConstants; +import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum; +import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException; + +/** + * OpenApi模块的异常 + * + * @author caiti + * @date 2022-11-11 + */ +public class OpenApiException extends ServiceException { + + public OpenApiException(AbstractExceptionEnum exceptionEnum) { + super(OpenApiConstants.OPENAPI_MODULE_NAME, exceptionEnum); + } + +} diff --git a/kernel-d-openapi/openapi-sdk-springdoc/pom.xml b/kernel-d-openapi/openapi-sdk-springdoc/pom.xml new file mode 100644 index 000000000..667e60db1 --- /dev/null +++ b/kernel-d-openapi/openapi-sdk-springdoc/pom.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>kernel-d-openapi</artifactId> + <groupId>cn.stylefeng.roses</groupId> + <version>7.3.2</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>openapi-sdk-springdoc</artifactId> + + <packaging>jar</packaging> + + <dependencies> + + <!-- openapi接口文档模块 --> + <dependency> + <groupId>cn.stylefeng.roses</groupId> + <artifactId>openapi-api</artifactId> + <version>${roses.version}</version> + </dependency> + + <dependency> + <groupId>org.springdoc</groupId> + <artifactId>springdoc-openapi-webmvc-core</artifactId> + </dependency> + + </dependencies> + + +</project> \ No newline at end of file diff --git a/kernel-d-openapi/openapi-sdk-springdoc/src/main/java/cn/stylefeng/roses/kernel/springdoc/converter/GunsModelConverterImpl.java b/kernel-d-openapi/openapi-sdk-springdoc/src/main/java/cn/stylefeng/roses/kernel/springdoc/converter/GunsModelConverterImpl.java new file mode 100644 index 000000000..a350dbeac --- /dev/null +++ b/kernel-d-openapi/openapi-sdk-springdoc/src/main/java/cn/stylefeng/roses/kernel/springdoc/converter/GunsModelConverterImpl.java @@ -0,0 +1,80 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.springdoc.converter; + +import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription; +import io.swagger.v3.core.converter.AnnotatedType; +import io.swagger.v3.core.converter.ModelConverter; +import io.swagger.v3.core.converter.ModelConverterContext; +import io.swagger.v3.oas.models.media.Schema; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Service; + +import java.lang.annotation.Annotation; +import java.util.Iterator; +import java.util.Objects; + +/** + * 文档转换 + * + * @author caiti + * @date 2022-11-11 + */ +public class GunsModelConverterImpl implements ModelConverter { + + @Override + public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context, Iterator<ModelConverter> chain) { + Schema schema = next(annotatedType, context, chain); + Annotation[] ctxAnnotations = annotatedType.getCtxAnnotations(); + if (Objects.isNull(ctxAnnotations)) { + return schema; + } + ChineseDescription chineseDescription = null; + for (Annotation ctxAnnotation : ctxAnnotations) { + if (ctxAnnotation instanceof ChineseDescription) { + chineseDescription = (ChineseDescription) ctxAnnotation; + break; + } + } + if (chineseDescription == null) { + return schema; + } + + if (schema != null) { + schema.setDescription(chineseDescription.value()); + } + return schema; + } + + private Schema next(AnnotatedType annotatedType, ModelConverterContext context, Iterator<ModelConverter> chain) { + if (chain.hasNext()) { + return chain.next().resolve(annotatedType, context, chain); + } else { + return null; + } + } + +} diff --git a/kernel-d-openapi/openapi-sdk-springdoc/src/main/java/cn/stylefeng/roses/kernel/springdoc/providers/GunsJavadocProviderImpl.java b/kernel-d-openapi/openapi-sdk-springdoc/src/main/java/cn/stylefeng/roses/kernel/springdoc/providers/GunsJavadocProviderImpl.java new file mode 100644 index 000000000..54f4f0be1 --- /dev/null +++ b/kernel-d-openapi/openapi-sdk-springdoc/src/main/java/cn/stylefeng/roses/kernel/springdoc/providers/GunsJavadocProviderImpl.java @@ -0,0 +1,107 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.springdoc.providers; + +import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription; +import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource; +import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource; +import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource; +import org.springdoc.core.providers.JavadocProvider; +import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.lang.Nullable; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Map; + +/** + * 文档转换 + * + * @author caiti + * @date 2022-11-11 + */ +public class GunsJavadocProviderImpl implements JavadocProvider { + + @Override + public String getClassJavadoc(Class<?> cl) { + ApiResource annotation = AnnotatedElementUtils.getMergedAnnotation(cl, ApiResource.class); + if (annotation != null) { + return annotation.name(); + } + return null; + } + + @Override + public String getMethodJavadocDescription(Method method) { + return getResourceName(method); + } + + @Override + public String getMethodJavadocReturn(Method method) { + return getResourceName(method); + } + + @Nullable + private String getResourceName(Method method) { + GetResource getAnnotation = AnnotatedElementUtils.getMergedAnnotation(method, GetResource.class); + if (getAnnotation != null) { + return getAnnotation.name(); + } + PostResource postAnnotation = AnnotatedElementUtils.getMergedAnnotation(method, PostResource.class); + if (postAnnotation != null) { + return postAnnotation.name(); + } + ApiResource apiAnnotation = AnnotatedElementUtils.getMergedAnnotation(method, ApiResource.class); + if (apiAnnotation != null) { + return apiAnnotation.name(); + } + return null; + } + + @Override + public Map<String, String> getMethodJavadocThrows(Method method) { + + return null; + } + + @Override + public String getParamJavadoc(Method method, String name) { + return null; + } + + @Override + public String getFieldJavadoc(Field field) { + ChineseDescription annotation = AnnotatedElementUtils.getMergedAnnotation(field, ChineseDescription.class); + if (annotation != null) { + return annotation.value(); + } + return null; + } + + @Override + public String getFirstSentence(String text) { + return text; + } +} diff --git a/kernel-d-openapi/openapi-spring-boot-starter/pom.xml b/kernel-d-openapi/openapi-spring-boot-starter/pom.xml new file mode 100644 index 000000000..0f32273f3 --- /dev/null +++ b/kernel-d-openapi/openapi-spring-boot-starter/pom.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>kernel-d-openapi</artifactId> + <groupId>cn.stylefeng.roses</groupId> + <version>7.3.2</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>openapi-spring-boot-starter</artifactId> + + <packaging>jar</packaging> + + + <dependencies> + + <!-- openapi接口文档springdoc实现 --> + <dependency> + <groupId>cn.stylefeng.roses</groupId> + <artifactId>openapi-sdk-springdoc</artifactId> + <version>${roses.version}</version> + </dependency> + + </dependencies> + +</project> \ No newline at end of file diff --git a/kernel-d-openapi/openapi-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/springdoc/starter/SpringdocOpenApiAutoConfiguration.java b/kernel-d-openapi/openapi-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/springdoc/starter/SpringdocOpenApiAutoConfiguration.java new file mode 100644 index 000000000..cc3f97a1f --- /dev/null +++ b/kernel-d-openapi/openapi-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/springdoc/starter/SpringdocOpenApiAutoConfiguration.java @@ -0,0 +1,56 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.springdoc.starter; + +import cn.stylefeng.roses.kernel.springdoc.converter.GunsModelConverterImpl; +import cn.stylefeng.roses.kernel.springdoc.providers.GunsJavadocProviderImpl; +import io.swagger.v3.core.converter.ModelConverter; +import org.springdoc.core.providers.JavadocProvider; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; + +/** + * openapi的自动装配 + * + * @author caiti + * @date 2022-11-11 + */ +@Configuration +public class SpringdocOpenApiAutoConfiguration { + + @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) + public ModelConverter gunsSpringdocModelConverter() { + return new GunsModelConverterImpl(); + } + + @Bean + public JavadocProvider gunsSpringdocJavadocProvider() { + return new GunsJavadocProviderImpl(); + } + +} diff --git a/kernel-d-openapi/openapi-spring-boot-starter/src/main/resources/META-INF/spring.factories b/kernel-d-openapi/openapi-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..0f4a72fe5 --- /dev/null +++ b/kernel-d-openapi/openapi-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + cn.stylefeng.roses.kernel.springdoc.starter.SpringdocOpenApiAutoConfiguration diff --git a/kernel-d-openapi/pom.xml b/kernel-d-openapi/pom.xml new file mode 100644 index 000000000..853d289b8 --- /dev/null +++ b/kernel-d-openapi/pom.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>roses-kernel</artifactId> + <groupId>cn.stylefeng.roses</groupId> + <version>7.3.2</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>kernel-d-openapi</artifactId> + + <packaging>pom</packaging> + + <modules> + <module>openapi-api</module> + <module>openapi-spring-boot-starter</module> + <module>openapi-sdk-springdoc</module> + </modules> + +</project> \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3ef537dbe..628158696 100644 --- a/pom.xml +++ b/pom.xml @@ -114,6 +114,7 @@ <!-- 数据迁移模块 --> <module>kernel-s-migration</module> + <module>kernel-d-openapi</module> </modules> @@ -155,6 +156,7 @@ <openfeign.version>2.2.6.RELEASE</openfeign.version> <seata.all.version>1.3.0</seata.all.version> <bcprov.version>1.68</bcprov.version> + <springdoc.version>1.6.12</springdoc.version> </properties> <dependencyManagement> @@ -328,6 +330,13 @@ <version>${getty.version}</version> </dependency> + <!-- openapi接口文档 --> + <dependency> + <groupId>org.springdoc</groupId> + <artifactId>springdoc-openapi-webmvc-core</artifactId> + <version>${springdoc.version}</version> + </dependency> + </dependencies> </dependencyManagement>