I am wondering whether there is an option in Swagger to authorize request to url containing external documentation.
I have the following configuration, what I am interested in is urls[1]
part
springdoc.swagger-ui.urls[0].url=/v3/api-docs
springdoc.swagger-ui.urls[0].name=default
springdoc.swagger-ui.urls[1].url=https://asdasd.blob.core.windows.net/my-api/docs.spec
springdoc.swagger-ui.urls[1].name=additional
I am able to access the docs from the server (curl directly from the machine) however from the Swagger (so the browser) I can't. I am wondering if there is an option to add eg. Authorization Bearer xxxx
to this request or actually make server issue the request, not the client.
To clarify - I want to fetch second file with OpenAPI definition from the remote server so I am talking about this:
Not the Authorize
part
Solution I implemented:
What Helen pointed out would work - intercepting the request and adding the headers - however unfortunately in my case there was also an issue connected to firewall settings of a storage. Only traffic coming from the server, not the client (browser) was passing through so I:
springdoc.swagger-ui.urls[1].url=v3/api-docs/additional
mydomain.swagger-ui.additionalDocsUrl=...
@Hidden
@RestController
@RequestMapping("/v3/api-docs/additional")
@RequiredArgsConstructor
public class AdditionalOpenApiController {
@Value("${mydomain.swagger-ui.additionalDocsUrl}")
private String additionalDocsUrl;
@Cacheable(value = "ADDITIONAL_API_DOCS", unless = "#result != null")
@GetMapping(produces = "application/json")
public String getAdditionalDocs() {
// in my case remote produces application/octet-stream so it needs to be byte[]
return Optional.ofNullable(new RestTemplate().getForObject(additionalDocsUrl, byte[].class))
.map(String::new)
.orElseThrow(() -> new ResourceNotFoundException("Cannot load Additional OpenAPI from storage"));
}
This can be achieved only programmatically.
requestInterceptor
: See on AbstractSwaggerIndexTransformer samples for implementing your request programmatically (for example addCSRF method).MySwaggerIndexPageTransformer
and define it as a spring bean and it will be loaded instead of the springdoc SwaggerIndexPageTransformer
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With