Skip to content

Commit

Permalink
新增 solon.docs.openapi2 插件
Browse files Browse the repository at this point in the history
  • Loading branch information
noear committed Jul 30, 2023
1 parent 73cd50b commit c8fab5b
Show file tree
Hide file tree
Showing 25 changed files with 1,557 additions and 124 deletions.
1 change: 1 addition & 0 deletions UPDATE_LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

### 2.4.2
* 新增 lettuce-solon-plugin 插件
* 新增 solon.docs.openapi2 插件
* 新增 solon.cloud.metrics 插件?
* 升级 solon-maven-plugin 的相关依赖
* 增加 solon-admin-server 对 basic auth 配置的支持
Expand Down
2 changes: 1 addition & 1 deletion __release/solon-base-bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<module>../../solon-projects/solon-base/solon.docs</module>
<module>../../solon-projects/solon-base/solon.docs.openapi2</module>
<module>../../solon-projects/solon-base/solon.docs.openapi3</module>
<!-- <module>../../solon-projects/solon-base/solon.docs.openapi3</module>-->

<module>../../solon-projects/solon-base/solon.auth</module>
<module>../../solon-projects/solon-base/solon.test</module>
Expand Down
7 changes: 1 addition & 6 deletions solon-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<snakeyaml.version>2.0</snakeyaml.version>

<swagger.version>1.6.11</swagger.version>
<swagger2.version>2.2.15</swagger2.version>
<swagger2.version>2.1.13</swagger2.version>
<knife4j.version>4.1.0</knife4j.version>

<jackson.version>2.14.3</jackson.version>
Expand Down Expand Up @@ -409,11 +409,6 @@
<artifactId>solon.docs.openapi2</artifactId>
<version>${solon.version}</version>
</dependency>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.docs.openapi3</artifactId>
<version>${solon.version}</version>
</dependency>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.auth</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.noear.solon.docs.openapi2;

import io.swagger.annotations.*;
import io.swagger.models.Contact;
import io.swagger.models.ExternalDocs;
import io.swagger.models.Info;
import io.swagger.models.License;
import io.swagger.models.Tag;
import io.swagger.models.*;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.*;
import io.swagger.models.properties.*;
import io.swagger.models.refs.RefFormat;
import io.swagger.solon.annotation.ApiNoAuthorize;
import io.swagger.solon.annotation.ApiRes;
import io.swagger.solon.annotation.ApiResProperty;
import org.noear.solon.Solon;
Expand All @@ -23,7 +24,12 @@
import org.noear.solon.docs.ApiEnum;
import org.noear.solon.docs.DocDocket;
import org.noear.solon.docs.exception.DocException;
import org.noear.solon.docs.models.ApiContact;
import org.noear.solon.docs.models.ApiLicense;
import org.noear.solon.docs.models.ApiScheme;
import org.noear.solon.docs.openapi2.impl.ActionHolder;
import org.noear.solon.docs.openapi2.impl.BuilderHelper;
import org.noear.solon.docs.openapi2.impl.ParamHolder;

import java.lang.reflect.*;
import java.text.Collator;
Expand All @@ -35,7 +41,7 @@
* @author noear
* @since 2.3
*/
public class Swagger2Builder {
public class OpenApi2Builder {
private final Swagger swagger = new Swagger();
private final DocDocket docket;

Expand All @@ -44,7 +50,7 @@ public class Swagger2Builder {
*/
private ModelImpl globalResultModel;

public Swagger2Builder(DocDocket docket) {
public OpenApi2Builder(DocDocket docket) {
this.docket = docket;
}

Expand All @@ -57,15 +63,34 @@ public Swagger build() {
// 解析JSON
this.parseGroupPackage();

ApiLicense apiLicense = docket.info().license();
ApiContact apiContact = docket.info().contact();

swagger.setSwagger(docket.version());
swagger.info(new Info()
.title(docket.info().title())
.description(docket.info().description())
.termsOfService(docket.info().termsOfService())
.version(docket.info().version())
.license(docket.info().license())
.contact(docket.info().contact()));
.version(docket.info().version()));

if (apiLicense != null) {
License license = new License()
.url(apiLicense.url())
.name(apiLicense.name());
license.setVendorExtensions(apiLicense.vendorExtensions());

swagger.getInfo().setLicense(license);
}

if (apiContact != null) {
Contact contact = new Contact()
.email(apiContact.email())
.name(apiContact.name())
.url(apiContact.url());
contact.setVendorExtensions(apiContact.vendorExtensions());
swagger.getInfo().contact(contact);
}


swagger.host(BuilderHelper.getHost(docket));
swagger.basePath(docket.basePath());
Expand All @@ -81,7 +106,7 @@ public Swagger build() {
}

swagger.vendorExtensions(docket.vendorExtensions());
swagger.setSecurityDefinitions(docket.securityDefinitions());
//swagger.setSecurityDefinitions(docket.securityDefinitions());

if (swagger.getTags() != null) {
//排序
Expand Down Expand Up @@ -253,12 +278,12 @@ private void parseAction(List<ActionHolder> actionHolders) {
operation.setDescription(apiAction.notes());
operation.setDeprecated(actionHolder.isAnnotationPresent(Deprecated.class));

if ((actionHolder.isAnnotationPresent(ApiNoAuthorize.class) ||
actionHolder.controllerClz().isAnnotationPresent(ApiNoAuthorize.class)) == false) {
for (String securityName : docket.securityDefinitions().keySet()) {
operation.security(new SecurityRequirement(securityName).scope("global"));
}
}
// if ((actionHolder.isAnnotationPresent(ApiNoAuthorize.class) ||
// actionHolder.controllerClz().isAnnotationPresent(ApiNoAuthorize.class)) == false) {
// for (String securityName : docket.securityDefinitions().keySet()) {
// operation.security(new SecurityRequirement(securityName).scope("global"));
// }
// }


String operationMethod = BuilderHelper.getHttpMethod(actionHolder, apiAction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static String getApiJson(Context ctx, String group) throws IOException {
docket.globalResponseCodes().put(200, "");
}

Swagger swagger = new Swagger2Builder(docket).build();
Swagger swagger = new OpenApi2Builder(docket).build();
return JsonUtil.toJson(swagger);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.noear.solon.docs.openapi2;
package org.noear.solon.docs.openapi2.impl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.noear.solon.docs.openapi2;
package org.noear.solon.docs.openapi2.impl;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.noear.solon.docs.openapi2;
package org.noear.solon.docs.openapi2.impl;

import io.swagger.annotations.ApiImplicitParam;
import org.noear.solon.Utils;
Expand Down
45 changes: 45 additions & 0 deletions solon-projects/solon-base/solon.docs.openapi3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.4.2-SNAPSHOT</version>
<relativePath>../../../solon-parent/pom.xml</relativePath>
</parent>

<artifactId>solon.docs.openapi3</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.docs</artifactId>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger2.version}</version>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger2.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit c8fab5b

Please sign in to comment.