Skip to content

New configuration properties #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
/**
* The type Abstract open api resource.
* @author bnasslahsen
* @author Maciej Kopeć
*/
public abstract class AbstractOpenApiResource extends SpecFilter {

Expand Down Expand Up @@ -215,18 +216,28 @@ public static void addHiddenRestControllers(Class<?>... classes) {
* @param classes the classes
*/
public static void addHiddenRestControllers(String... classes) {
Set<Class<?>> hiddenClasses =new HashSet<>();
Set<Class<?>> hiddenClasses = new HashSet<>();
for (String aClass : classes) {
try {
hiddenClasses.add(Class.forName(aClass));
}
catch (ClassNotFoundException e) {
LOGGER.warn("The following class doesn't exist and cannot be hidden: {}", aClass);
LOGGER.warn("The following class doesn't exist and cannot be hidden: {}", aClass);
}
}
HIDDEN_REST_CONTROLLERS.addAll(hiddenClasses);
}

/**
* Is hidden rest controllers boolean.
*
* @param rawClass the raw class
* @return the boolean
*/
public static boolean isHiddenRestControllers(Class<?> rawClass) {
return HIDDEN_REST_CONTROLLERS.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
}

/**
* Gets open api.
*
Expand All @@ -252,7 +263,7 @@ protected synchronized OpenAPI getOpenApi() {
// run the optional customisers
openApiCustomisers.ifPresent(apiCustomisers -> apiCustomisers.forEach(openApiCustomiser -> openApiCustomiser.customise(openApi)));
if (!CollectionUtils.isEmpty(openApi.getServers()))
openAPIBuilder.setServersPresent(true);
openAPIBuilder.setServersPresent(!springDocConfigProperties.isAlwaysAddGeneratedServer());
openAPIBuilder.updateServers(openApi);

if (springDocConfigProperties.isRemoveBrokenReferenceDefinitions())
Expand Down Expand Up @@ -590,16 +601,6 @@ protected boolean isAdditionalRestController(Class<?> rawClass) {
return ADDITIONAL_REST_CONTROLLERS.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
}

/**
* Is hidden rest controllers boolean.
*
* @param rawClass the raw class
* @return the boolean
*/
public static boolean isHiddenRestControllers(Class<?> rawClass) {
return HIDDEN_REST_CONTROLLERS.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
}

/**
* Gets default allowed http methods.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -75,6 +76,7 @@
/**
* The type Open api builder.
* @author bnasslahsen
* @author Maciej Kopeć
*/
public class OpenAPIBuilder {

Expand Down Expand Up @@ -223,10 +225,15 @@ else if (calculatedOpenAPI.getInfo() == null) {
public OpenAPI updateServers(OpenAPI openAPI) {
if (!isServersPresent) // default server value
{
Server server = new Server().url(serverBaseUrl).description(DEFAULT_SERVER_DESCRIPTION);
List<Server> servers = new ArrayList();
servers.add(server);
openAPI.setServers(servers);
final Server generatedServer = new Server()
.url(serverBaseUrl + springDocConfigProperties.getGeneratedServerSuffix())
.description(DEFAULT_SERVER_DESCRIPTION);

final List<Server> servers = ObjectUtils.defaultIfNull(openAPI.getServers(), new ArrayList<>());

if (!servers.contains(generatedServer)) {
openAPI.addServersItem(generatedServer);
}
}
return openAPI;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
/**
* The type Spring doc config properties.
* @author bnasslahsen
* @author Maciej Kopeć
*/
@Configuration
@ConfigurationProperties(prefix = Constants.SPRINGDOC_PREFIX)
Expand Down Expand Up @@ -120,6 +121,16 @@ public class SpringDocConfigProperties {
*/
private String defaultProducesMediaType = MediaType.ALL_VALUE;

/**
* Always add a generated server to server's list
*/
private boolean alwaysAddGeneratedServer = false;

/**
* Add a suffix to a generated server's host.
*/
private String generatedServerSuffix = "";

/**
* Is auto tag classes boolean.
*
Expand Down Expand Up @@ -426,6 +437,22 @@ public void setWriterWithDefaultPrettyPrinter(boolean writerWithDefaultPrettyPri
this.writerWithDefaultPrettyPrinter = writerWithDefaultPrettyPrinter;
}

public boolean isAlwaysAddGeneratedServer() {
return alwaysAddGeneratedServer;
}

public void setAlwaysAddGeneratedServer(boolean alwaysAddGeneratedServer) {
this.alwaysAddGeneratedServer = alwaysAddGeneratedServer;
}

public String getGeneratedServerSuffix() {
return generatedServerSuffix;
}

public void setGeneratedServerSuffix(String generatedServerSuffix) {
this.generatedServerSuffix = generatedServerSuffix;
}

/**
* The type Webjars.
* @author bnasslahsen
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.app124;

import org.junit.jupiter.api.Test;
import org.springdoc.core.Constants;
import test.org.springdoc.api.AbstractSpringDocTest;

import org.springframework.test.context.TestPropertySource;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static test.org.springdoc.api.app124.SpringDocTestApp.SERVER_HOSTNAME;

/**
* @author Maciej Kopeć
*/
@TestPropertySource(properties = "springdoc.always-add-generated-server=true")
public class SpringDocApp124Test extends AbstractSpringDocTest {

@Test
public void testAlwaysAddGeneratedServer() throws Exception {
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk())
.andExpect(jsonPath("$.openapi", is("3.0.1")))
.andExpect(jsonPath("$.servers", hasSize(2)))
.andExpect(jsonPath("$.servers[0].url", is(SERVER_HOSTNAME)))
.andExpect(jsonPath("$.servers[1].url", is("http://localhost")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.app124;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.servers.Server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
* @author Maciej Kopeć
*/
@SpringBootApplication
public class SpringDocTestApp {

public static final String SERVER_HOSTNAME = "https:://hostname.org/";

public static void main(String[] args) {
SpringApplication.run(SpringDocTestApp.class, args);
}

@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.addServersItem(
new Server()
.url(SERVER_HOSTNAME)
.description("Super API")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.app125;

import org.junit.jupiter.api.Test;
import org.springdoc.core.Constants;
import test.org.springdoc.api.AbstractSpringDocTest;

import org.springframework.test.context.TestPropertySource;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* @author Maciej Kopeć
*/
@TestPropertySource(properties = "springdoc.generated-server-suffix=/suffix")
public class SpringDocApp125Test extends AbstractSpringDocTest {

@Test
public void testAddSuffixToGeneratedServer() throws Exception {
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk())
.andExpect(jsonPath("$.openapi", is("3.0.1")))
.andExpect(jsonPath("$.servers", hasSize(1)))
.andExpect(jsonPath("$.servers[0].url", is("http://localhost/suffix")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * 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
* *
* * https://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.
*
*/

package test.org.springdoc.api.app125;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author Maciej Kopeć
*/
@SpringBootApplication
public class SpringDocTestApp {

public static void main(String[] args) {
SpringApplication.run(SpringDocTestApp.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url":"https:://hostname.org/",
"description":"Super API"
},
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {},
"components": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost/suffix",
"description": "Generated server url"
}
],
"paths": {},
"components": {}
}