make API page work with swagger 3

This commit is contained in:
MiniDigger 2020-07-14 17:02:33 +02:00
parent ba83a30fe4
commit 948460e862
5 changed files with 103 additions and 112 deletions

View File

@ -53,7 +53,7 @@
<!--SpringFox dependencies -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
@ -63,11 +63,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<!-- Bean Validation API support -->
<dependency>

View File

@ -59,7 +59,6 @@ public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// TODO we kinda want this to be /assets, via the hangar.url macro maybe?
registry.addResourceHandler("/assets/**")
.addResourceLocations("classpath:/public/")
.setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS))

View File

@ -13,10 +13,10 @@ import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2WebMvc
@EnableSwagger2
public class SwaggerConfig {
ApiInfo apiInfo() {
@ -49,7 +49,7 @@ public class SwaggerConfig {
@Bean
public Docket customImplementation() {
return new Docket(DocumentationType.SWAGGER_2)
return new Docket(DocumentationType.OAS_30)
.select()
.apis(RequestHandlerSelectors.basePackage("me.minidigger.hangar"))
.build()

View File

@ -68,8 +68,8 @@ public class ApplicationController extends HangarController {
}
@RequestMapping("/api")
public Object swagger() {
return null; // TODO implement swagger request controller
public ModelAndView swagger() {
return fillModel(new ModelAndView("swagger"));
}
@RequestMapping("/favicon.ico")
@ -116,9 +116,9 @@ public class ApplicationController extends HangarController {
return null; // TODO implement sitemapIndex request controller
}
@RequestMapping("/{path}/")
public Object removeTrail(@PathVariable Object path) {
return null; // TODO implement removeTrail request controller
}
// @RequestMapping("/{path}/")
// public Object removeTrail(@PathVariable Object path) {
// return null; // implement removeTrail request controller - pretty sure this one is dum
// }
}

View File

@ -1,102 +1,99 @@
@import controllers.sugar.Requests.OreRequest
@import views.html.helper.CSPNonce
@()(implicit messages: Messages, request: OreRequest[_], assetsFinder: AssetsFinder)
@*
<#--
Mostly copied from swagger-ui's index file
*@
-->
<#import "/spring.ftl" as spring />
<#import "*/utils/hangar.ftlh" as hangar />
<#import "*/layout/base.ftlh" as base />
<!DOCTYPE html>
<html lang="@messages.lang.language">
<head>
<title><@spring.message "general.openapi" /></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="<@hangar.url "images/favicon.ico" />">
<link rel="stylesheet" type="text/css" href="<@hangar.url "lib/swagger-ui/swagger-ui.css" />">
<style>
html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
margin:0;
background: #fafafa;
}
.swagger-ui .topbar .download-url-wrapper, .swagger-ui .info hgroup.main a {
display: none;
}
.swagger-ui .info {
margin: 2rem 0;
}
.model-container, .responses-inner {
overflow-x: auto;
}
.swagger-ui .info .description h2 {
padding-top: 1.5rem;
margin: 1.5rem 0 0;
border-top: 3px solid #333333;
}
.swagger-ui .scheme-container {
border-top: 1px solid rgba(0,0,0,.15);
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script type="text/javascript" src="<@hangar.url "lib/jquery/dist/jquery.min.js" />"></script>
<script src="<@hangar.url "lib/swagger-ui/swagger-ui-bundle.js" />"></script>
<script src="<@hangar.url "lib/swagger-ui/swagger-ui-standalone-preset.js" />"></script>
<script src="<@hangar.url "javascripts/apiRequests.js" />"></script>
<script @CSPNonce.attr>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: "<@hangar.url "swagger.json" />",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
requestInterceptor: (req) => {
if (!req.loadSpec) {
const promise = getApiSession().then((session) => {
req.headers.authorization = 'HangarApi session=' + session;
return req;
});
// Workaround for fixing the curl URL
// https://github.com/swagger-api/swagger-ui/issues/4778#issuecomment-456403631
promise.url = req.url;
return promise;
} else {
return req;
}
}
})
<#macro scripts>
<script type="text/javascript" src="<@hangar.url "lib/jquery/dist/jquery.min.js" />"></script>
<script src="<@hangar.url "lib/swagger-ui/swagger-ui-bundle.js" />"></script>
<script src="<@hangar.url "lib/swagger-ui/swagger-ui-standalone-preset.js" />"></script>
<script src="<@hangar.url "javascripts/apiRequests.js" />"></script>
<script @CSPNonce.attr>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: "/v2/api-docs",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "BaseLayout",
requestInterceptor: (req) => {
if (!req.loadSpec) {
const promise = getApiSession().then((session) => {
req.headers.authorization = 'HangarApi session=' + session;
return req;
});
// Workaround for fixing the curl URL
// https://github.com/swagger-api/swagger-ui/issues/4778#issuecomment-456403631
promise.url = req.url;
return promise;
} else {
return req;
}
}
</script>
<#if request??>
<script @CSPNonce.attr>
csrf = '@play.filters.csrf.CSRF.getToken.get.value';
isLoggedIn = ${request.hasUser};
</script>
</#if>
</body>
</html>
})
}
</script>
<#if request??>
<script @CSPNonce.attr>
csrf = '@play.filters.csrf.CSRF.getToken.get.value';
isLoggedIn = ${request.hasUser};
</script>
</#if>
</#macro>
<#macro stylesheets>
<link rel="stylesheet" type="text/css" href="<@hangar.url "lib/swagger-ui/swagger-ui.css" />">
<style>
html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
margin:0;
background: #fafafa;
}
.swagger-ui .topbar .download-url-wrapper, .swagger-ui .info hgroup.main a {
display: none;
}
.swagger-ui .info {
margin: 2rem 0;
}
.swagger-ui .info .title small pre {
background-color: unset;
border: unset;
}
.model-container, .responses-inner {
overflow-x: auto;
}
.swagger-ui .info .description h2 {
padding-top: 1.5rem;
margin: 1.5rem 0 0;
border-top: 3px solid #333333;
}
.swagger-ui .scheme-container {
border-top: 1px solid rgba(0,0,0,.15);
}
</style>
</#macro>
<#assign message><@spring.message "general.openapi" /></#assign>
<#assign scriptsVar><@scripts /></#assign>
<#assign stylesheetsVar><@stylesheets /></#assign>
<@base.base title="${message}" additionalScripts=scriptsVar additionalStyling=stylesheetsVar>
<div id="swagger-ui"></div>
</@base.base>