Skip to content

Commit

Permalink
Merge branch 'smou-2.0.4/add-setting-swagger-ui-url' into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Apr 16, 2023
2 parents 992065f + f8aa2e4 commit 5e5234f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
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));
}
}

0 comments on commit 5e5234f

Please sign in to comment.