Skip to content
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

Add support for swagger-ui.url property #2189

Merged
merged 1 commit into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ protected String overwriteSwaggerDefaultUrl(String html) {
return html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);
}

/**
* Setting the url configured with swagger ui properties
*
* @param html
* @return modifed html
*/
protected String setConfiguredApiDocsUrl(String html){
return html.replace(Constants.SWAGGER_UI_DEFAULT_URL, swaggerUiConfig.getUrl());
}

/**
* Default transformations string.
*
Expand Down Expand Up @@ -167,6 +177,10 @@ else if (swaggerUiConfig.getCsrf().isUseSessionStorage())
if (swaggerUiConfig.isDisableSwaggerDefaultUrl())
html = overwriteSwaggerDefaultUrl(html);

if(StringUtils.isNotEmpty(swaggerUiConfig.getUrl())){
html = setConfiguredApiDocsUrl(html);
}

return html;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.springdoc.ui;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springdoc.core.properties.SwaggerUiConfigParameters;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springdoc.core.properties.SwaggerUiOAuthProperties;
import org.springdoc.core.providers.ObjectMapperProvider;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

@ExtendWith(MockitoExtension.class)
public class AbstractSwaggerIndexTransformerTest {

private SwaggerUiConfigProperties swaggerUiConfig;
@Mock
private SwaggerUiOAuthProperties swaggerUiOAuthProperties;
@Mock
private SwaggerUiConfigParameters swaggerUiConfigParameters;
@Mock
private ObjectMapperProvider objectMapperProvider;

private AbstractSwaggerIndexTransformer underTest;

private final String swaggerInitJs = "window.onload = function() {\n" +
"\n" +
" window.ui = SwaggerUIBundle({\n" +
" url: \"https://petstore.swagger.io/v2/swagger.json\",\n" +
" dom_id: '#swagger-ui',\n" +
" deepLinking: true,\n" +
" presets: [\n" +
" SwaggerUIBundle.presets.apis,\n" +
" SwaggerUIStandalonePreset\n" +
" ],\n" +
" plugins: [\n" +
" SwaggerUIBundle.plugins.DownloadUrl\n" +
" ],\n" +
" layout: \"StandaloneLayout\"\n" +
" });\n" +
"\n" +
" //</editor-fold>\n" +
"};";
private final InputStream is = new ByteArrayInputStream(swaggerInitJs.getBytes());

private final String apiDocUrl = "http://test.springdoc.com/apidoc";

@BeforeEach
public void setup(){
swaggerUiConfig = new SwaggerUiConfigProperties();
swaggerUiConfig.setUrl(apiDocUrl);
underTest = new AbstractSwaggerIndexTransformer(swaggerUiConfig, swaggerUiOAuthProperties, swaggerUiConfigParameters, objectMapperProvider);

}

@Test
void setApiDocUrlCorrectly() throws IOException {
var html = underTest.defaultTransformations(is);
assertThat(html, containsString(apiDocUrl));
}
}