Skip to content

When I added a GetMapping for /v3/api-docs, it caused an exception. #2341

@ok1996

Description

@ok1996

Describe the bug

When there are two possible URLs, such as /v3/api-docs and /${spring.application.name}/v3/api-docs,it might lead to an exception during the requestUrl.substring operation if the requestUrl used during access doesn't include ${spring.application.name}.
In such cases, the length of the prefix would exceed the length of the requestUrl, causing an issue.

To Reproduce
Steps to reproduce the behavior:

I added the following code for forwarding in the service layer:

@Controller
public class AggregatedDocGatewayForward {

    @GetMapping("/${spring.application.name}/v3/api-docs/{param}")
    public String forwardV3ApiDocsParam(@PathVariable String param) {
        return "forward:/v3/api-docs/" + param;
    }

    @GetMapping(value = {"/${spring.application.name}/v3/api-docs"
            , "/${spring.application.name}/v3/api-docs/default"})
    public String forwardV3ApiDocs() {
        return "forward:/v3/api-docs";
    }

}

The following code may occasionally result in an exception:

org.springdoc.webmvc.api.OpenApiWebMvcResource:
  
	@Override
	protected String getServerUrl(HttpServletRequest request, String apiDocsUrl) {
		String requestUrl = decode(request.getRequestURL().toString());
		Optional<SpringWebProvider> springWebProviderOptional = springDocProviders.getSpringWebProvider();
		String prefix = StringUtils.EMPTY;
		if (springWebProviderOptional.isPresent())
			prefix = springWebProviderOptional.get().findPathPrefix(springDocConfigProperties);
		return requestUrl.substring(0, requestUrl.length() - apiDocsUrl.length() - prefix.length());
	}

Screenshots
2023-08-10_14-07-58

2023-08-10_14-08-22

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions