Skip to content

Commit

Permalink
chore(core): allow missing WebMvcConfigurer class in classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback authored and sam0r040 committed Jun 7, 2024
1 parent 21defb7 commit e865bc0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,12 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.core.configuration;

import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@RequiredArgsConstructor
// Highest so that all others will replace this configuration
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class SpringwolfUiResourceConfiguration implements WebMvcConfigurer {

private final SpringwolfConfigProperties springwolfConfigProperties;
private final WebProperties webProperties;
private final WebMvcProperties webMvcProperties;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(springwolfConfigProperties.getPath().getBase() + "/**", webMvcProperties.getStaticPathPattern())
.addResourceLocations(buildStaticLocation());
}

private String[] buildStaticLocation() {
List<String> staticLocations = new ArrayList<>(Arrays.asList(webProperties.getResources().getStaticLocations()));
staticLocations.add("classpath:/META-INF/resources/springwolf/");

return staticLocations.toArray(new String[0]);
}

}
@ConditionalOnClass(WebMvcConfigurer.class)
@Import(SpringwolfUiResourceConfigurer.class)
public class SpringwolfUiResourceConfiguration {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.core.configuration;

import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@RequiredArgsConstructor
@Order(value = Ordered.HIGHEST_PRECEDENCE) // Highest so that all others will replace this configuration
public class SpringwolfUiResourceConfigurer implements WebMvcConfigurer {

private final SpringwolfConfigProperties springwolfConfigProperties;
private final WebProperties webProperties;
private final WebMvcProperties webMvcProperties;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(
springwolfConfigProperties.getPath().getBase() + "/**", webMvcProperties.getStaticPathPattern())
.addResourceLocations(buildStaticLocation());
}

private String[] buildStaticLocation() {
List<String> staticLocations =
new ArrayList<>(Arrays.asList(webProperties.getResources().getStaticLocations()));
staticLocations.add("classpath:/META-INF/resources/springwolf/");

return staticLocations.toArray(new String[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public enum InitMode {
@Nullable
private Payload payload = new Payload();


@Getter
@Setter
public static class Paths {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public class AsyncApiController {
private final AsyncApiSerializerService serializer;

@GetMapping(
path = {"${springwolf.paths.docs:/springwolf/docs}",
"${springwolf.paths.docs:/springwolf/docs}.json",
"${springwolf.path.base:/springwolf}/${springwolf.path.docs:/docs}",
"${springwolf.path.base:/springwolf}/${springwolf.path.docs:/docs}.json"},
path = {
"${springwolf.paths.docs:/springwolf/docs}",
"${springwolf.paths.docs:/springwolf/docs}.json",
"${springwolf.path.base:/springwolf}/${springwolf.path.docs:/docs}",
"${springwolf.path.base:/springwolf}/${springwolf.path.docs:/docs}.json"
},
produces = MediaType.APPLICATION_JSON_VALUE)
public String asyncApiJson() throws JsonProcessingException {
log.debug("Returning AsyncApi.json document");
Expand All @@ -31,10 +33,12 @@ public String asyncApiJson() throws JsonProcessingException {
return serializer.toJsonString(asyncAPI);
}

@GetMapping(path = {
"${springwolf.paths.docs:/springwolf/docs}.yaml",
"${springwolf.path.base:/springwolf}/${springwolf.path.docs:/docs}.yaml"
}, produces = "application/yaml")
@GetMapping(
path = {
"${springwolf.paths.docs:/springwolf/docs}.yaml",
"${springwolf.path.base:/springwolf}/${springwolf.path.docs:/docs}.yaml"
},
produces = "application/yaml")
public String asyncApiYaml() throws JsonProcessingException {
log.debug("Returning AsyncApi.yaml document");

Expand Down

0 comments on commit e865bc0

Please sign in to comment.