Skip to content

Commit

Permalink
Replace springfox with springdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlkoch committed Nov 14, 2022
1 parent aaf8a4b commit c3ff378
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 178 deletions.
20 changes: 4 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@
<hibernate-extra-types.version>2.20.0</hibernate-extra-types.version>

<!-- Swagger/REST -->
<springfox.version>3.0.0</springfox.version>
<swagger.version>2.1.1</swagger.version>
<swagger-codegen-maven-plugin.version>2.4.29</swagger-codegen-maven-plugin.version>
<springdoc-openapi.version>1.6.12</springdoc-openapi.version>

<!-- JSON/Dataformats -->
<jackson-datatype-jts.version>1.0-2.7</jackson-datatype-jts.version>
Expand Down Expand Up @@ -160,11 +158,6 @@
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</plugin>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${swagger-codegen-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down Expand Up @@ -378,14 +371,9 @@

<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${springfox.version}</version>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc-openapi.version}</version>
</dependency>

<!-- Spring -->
Expand Down
4 changes: 2 additions & 2 deletions shogun-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@

<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
</dependency>

<!-- GeoServer Manager-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,23 @@
package de.terrestris.shogun.boot.config;

import de.terrestris.shogun.lib.config.SwaggerConfig;
import io.swagger.v3.oas.models.OpenAPI;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;

import java.util.Collections;
import java.util.function.Predicate;

@Configuration
@EnableAutoConfiguration
@Profile(value = {"!test"})
public class BootSwaggerConfig extends SwaggerConfig {

@Override
protected ApiInfo apiInfo() {
return new ApiInfo(
"SHOGun Boot REST-API",
description,
version,
termsOfServiceUrl,
contact,
license,
licenseUrl,
Collections.emptyList()
);
}
protected OpenAPI apiInfo() {
OpenAPI api = super.apiInfo();

@Override
protected Predicate<String> setSecurityContextPaths() {
return PathSelectors.any();
api.getInfo().setTitle("SHOGun Boot REST-API");

return api;
}

}
4 changes: 2 additions & 2 deletions shogun-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@

<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
</dependency>

<!-- Lombok -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ default void customHttpConfiguration(HttpSecurity http) throws Exception {
// Enable anonymous access to swagger docs
"/swagger-ui/index.html",
"/swagger-ui/**",
"/webjars/springfox-swagger-ui/**",
"/swagger-resources/**",
"/v2/api-docs",
"/v3/api-docs",
"/v3/api-docs/swagger-config",
// Enable anonymous access to GraphiQL
"/graphiql/**"
)
.permitAll()
Expand Down
2 changes: 0 additions & 2 deletions shogun-config/src/main/resources/log4j2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ Configuration:
level: info
- name: io.netty
level: info
- name: springfox.documentation
level: info
- name: org.thymeleaf
level: info
- name: org.hibernate
Expand Down
4 changes: 2 additions & 2 deletions shogun-gs-interceptor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@

<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
</dependency>

<!-- GeoServer Manager-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,21 @@
package de.terrestris.shogun.interceptor.config;

import de.terrestris.shogun.lib.config.SwaggerConfig;
import io.swagger.v3.oas.models.OpenAPI;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;

import java.util.Collections;
import java.util.function.Predicate;

@Configuration
@EnableAutoConfiguration
public class InterceptorSwaggerConfig extends SwaggerConfig {

@Override
protected ApiInfo apiInfo() {
return new ApiInfo(
"SHOGun GeoServer Interceptor REST-API",
description,
version,
termsOfServiceUrl,
contact,
license,
licenseUrl,
Collections.emptyList()
);
}
protected OpenAPI apiInfo() {
OpenAPI api = super.apiInfo();

@Override
protected Predicate<String> setSecurityContextPaths() {
return PathSelectors.any();
api.getInfo().setTitle("SHOGun GeoServer Interceptor REST-API");

return api;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,79 @@
*/
package de.terrestris.shogun.lib.config;

import com.fasterxml.classmate.TypeResolver;
import de.terrestris.shogun.lib.annotation.JsonSuperType;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springdoc.core.SpringDocUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.AlternateTypeRules;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.*;
import java.util.function.Predicate;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableAutoConfiguration
@Profile(value = {"!test"})
public abstract class SwaggerConfig {

@Autowired
private TypeResolver typeResolver;

protected String title = "SHOGun REST API";
protected String description = "This is the REST API description of SHOGun";
protected String version = "1.0.0";
protected String termsOfServiceUrl = "https://www.terrestris.de/en/impressum/";
protected Contact contact = new Contact("terrestris GmbH & Co. KG", "www.terrestris.de", "info@terrestris.de");
protected String contactName = "terrestris GmbH & Co. KG";
protected String contactUrl = "www.terrestris.de";
protected String contactMail = "info@terrestris.de";
protected String license = "Apache License, Version 2.0";
protected String licenseUrl = "https://www.apache.org/licenses/LICENSE-2.0.txt";

@Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.securityContexts(Collections.singletonList(actuatorSecurityContext()))
.securitySchemes(Collections.singletonList(apiKey()));

directModelSubsitutions(docket);

return docket;
}

private SecurityContext actuatorSecurityContext() {
return SecurityContext.builder()
.securityReferences(Collections.singletonList(bearerTokenAuth()))
.forPaths(setSecurityContextPaths()::test)
.build();
}
protected OpenAPI apiInfo() {
OpenAPI api = new OpenAPI()
.info(
new Info()
.title(title)
.description(description)
.version(version)
.contact(
new Contact()
.name(contactName)
.url(contactUrl)
.email(contactMail)
)
.license(
new License()
.name(license)
.url(licenseUrl)
)
)
.components(
// TODO Make list of endpoints configurable?
new Components()
.addSecuritySchemes(
"bearer-key",
new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
)
);

private SecurityScheme apiKey() {
return new ApiKey("apiKey", "Authorization", "header");
}
replaceJsonSuperTypes();

private SecurityReference bearerTokenAuth() {
return new SecurityReference("apiKey", new AuthorizationScope[0]);
return api;
}

protected void directModelSubsitutions(Docket docket) {
protected void replaceJsonSuperTypes() {
var reflections = new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forJavaClassPath())
.setScanners(
Expand Down Expand Up @@ -136,63 +139,7 @@ protected void directModelSubsitutions(Docket docket) {
Class<?> superType = entry.getKey();
Class<?> cl = entry.getValue();

docket.directModelSubstitute(superType, cl);
docket.alternateTypeRules(
AlternateTypeRules.newRule(
typeResolver.resolve(List.class, superType),
typeResolver.resolve(List.class, cl)
),
AlternateTypeRules.newRule(
typeResolver.resolve(Set.class, superType),
typeResolver.resolve(Set.class, cl)
),
AlternateTypeRules.newRule(
typeResolver.resolve(Collection.class, superType),
typeResolver.resolve(Collection.class, cl)
)
);
SpringDocUtils.getConfig().replaceWithClass(superType, cl);
}
}

protected ApiInfo apiInfo() {
return new ApiInfo(
title,
description,
version,
termsOfServiceUrl,
contact,
license,
licenseUrl,
Collections.emptyList()
);
}

/**
* Define the project specific paths that require BasicAuth authentication.
*
* Possible return values could be:
* <ul>
* <li>{@link PathSelectors#none()}</li>
* <li>{@link PathSelectors#any()}</li>
* <li>{@link PathSelectors#regex(String)}</li>
* <li>{@link PathSelectors#ant(String)}</li>
* </ul>
*
* Some examples for specifying a custom list of endpoints:
* <pre>
* PathSelectors.ant("/files/**");
* </pre>
*
* <pre>
* Predicates.or(
* PathSelectors.ant("/files/**"),
* PathSelectors.ant("/applications/**")
* );
* </pre>
*
* The latter requires {@code com.google.guava} on the classpath.
*
* @return The predicate that defines the secured paths.
*/
protected abstract Predicate<String> setSecurityContextPaths();
}
Loading

0 comments on commit c3ff378

Please sign in to comment.