|
15 | 15 | */
|
16 | 16 | package io.micronaut.openapi.view;
|
17 | 17 |
|
| 18 | +import io.micronaut.core.annotation.NonNull; |
18 | 19 | import io.micronaut.core.annotation.Nullable;
|
| 20 | +import io.micronaut.core.util.CollectionUtils; |
19 | 21 | import io.micronaut.core.util.StringUtils;
|
20 | 22 | import io.micronaut.inject.visitor.VisitorContext;
|
21 | 23 | import io.micronaut.openapi.view.OpenApiViewConfig.RendererType;
|
|
30 | 32 |
|
31 | 33 | import static io.micronaut.core.util.StringUtils.EMPTY_STRING;
|
32 | 34 | import static io.micronaut.openapi.view.OpenApiViewConfig.replacePlaceHolder;
|
| 35 | +import static io.micronaut.openapi.visitor.StringUtil.CLOSE_BRACE; |
33 | 36 | import static io.micronaut.openapi.visitor.StringUtil.COMMA_NEW_LINE;
|
34 | 37 | import static io.micronaut.openapi.visitor.StringUtil.KEY_VALUE_SEPARATOR;
|
| 38 | +import static io.micronaut.openapi.visitor.StringUtil.OPEN_BRACE; |
35 | 39 | import static io.micronaut.openapi.visitor.StringUtil.SLASH;
|
36 | 40 |
|
37 | 41 | /**
|
@@ -88,7 +92,7 @@ public final class ScalarConfig extends AbstractViewConfig {
|
88 | 92 | VALID_OPTIONS.put("hideClientButton", AbstractViewConfig::asBoolean);
|
89 | 93 |
|
90 | 94 | DEFAULT_OPTIONS.put("defaultHttpClient", "{targetKey: 'shell', clientKey: 'curl'}");
|
91 |
| - // by default, we have only shell curl (currl), Invoke-WebRequest Powershell (webrequest), java.net.http Java (nethttp), Fetch JavaScript (fetch), Axios JavaScript (axios) |
| 95 | + // by default, we have only shell curl (curl), Invoke-WebRequest Powershell (webrequest), java.net.http Java (nethttp), Fetch JavaScript (fetch), Axios JavaScript (axios) |
92 | 96 | DEFAULT_OPTIONS.put("hiddenClients", "['libcurl', 'clj_http', 'httpclient', 'restsharp', 'native', 'http1.1', 'asynchttp', 'okhttp', 'unirest', 'xhr', 'jquery', 'native', 'request', 'unirest', 'ofetch', 'nsurlsession', 'cohttp', 'guzzle', 'http', 'http1', 'http2', 'restmethod', 'python3', 'requests', 'httr', 'native', 'curl', 'httpie', 'wget', 'nsurlsession', 'undici'],");
|
93 | 97 | }
|
94 | 98 |
|
@@ -126,9 +130,37 @@ static ScalarConfig fromProperties(Map<String, String> properties, Map<Pair<Stri
|
126 | 130 | @Override
|
127 | 131 | public String render(String template, @Nullable VisitorContext context) {
|
128 | 132 | template = rapiPDFConfig.render(template, RendererType.SCALAR, context);
|
129 |
| - template = replacePlaceHolder(template, "scalar.js.url.prefix", isDefaultJsUrl ? getFinalUrlPrefix(RendererType.SCALAR, context) : jsUrl, StringUtils.EMPTY_STRING); |
130 |
| - template = replacePlaceHolder(template, "style", StringUtils.isNotEmpty(style) ? "<style>" + style + "</style>" : EMPTY_STRING, EMPTY_STRING); |
131 |
| - return replacePlaceHolder(template, "scalar.attributes", toOptions(), StringUtils.EMPTY_STRING); |
| 133 | + template = replacePlaceHolder(template, "scalar.js.url.prefix", isDefaultJsUrl ? getFinalUrlPrefix(RendererType.SCALAR, context) : jsUrl); |
| 134 | + template = replacePlaceHolder(template, "style", StringUtils.isNotEmpty(style) ? "<style>" + style + "</style>" : EMPTY_STRING); |
| 135 | + template = replacePlaceHolder(template, "scalar.sources", getSourcesStr()); |
| 136 | + return replacePlaceHolder(template, "scalar.attributes", toOptions()); |
| 137 | + } |
| 138 | + |
| 139 | + @NonNull |
| 140 | + private String getSourcesStr() { |
| 141 | + var result = new StringBuilder("sources: ["); |
| 142 | + // case with single document |
| 143 | + if (CollectionUtils.isEmpty(urls) || (withUrls != null && !withUrls)) { |
| 144 | + result.append("{url: contextPath + \"").append(specUrl).append("\"}"); |
| 145 | + } else { |
| 146 | + // case with multiple documents |
| 147 | + var isFirst = true; |
| 148 | + for (var url : urls) { |
| 149 | + if (!isFirst) { |
| 150 | + result.append(','); |
| 151 | + } |
| 152 | + result.append(OPEN_BRACE) |
| 153 | + .append("title: \"").append(url.name()).append("\",") |
| 154 | + .append("url: contextPath + \"").append(url.url()).append("\","); |
| 155 | + if (StringUtils.isNotEmpty(primaryName) && primaryName.equals(url.name())) { |
| 156 | + result.append("default: true"); |
| 157 | + } |
| 158 | + result.append(CLOSE_BRACE); |
| 159 | + isFirst = false; |
| 160 | + } |
| 161 | + } |
| 162 | + result.append("],"); |
| 163 | + return result.toString(); |
132 | 164 | }
|
133 | 165 |
|
134 | 166 | @Override
|
|
0 commit comments