Optional path variables are marked as required
See original GitHub issueHello,
It seems that the required
parameter of the @PathVariable
annotation is not being respected. The generated spec always marks the parameter as being required.
To Reproduce The following class demonstrates the issue. This is from a stock Spring Boot project, with only the Web starter and Springdoc as dependencies:
package com.cyberscout.springdocbug;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping(path = "/{param}", method = GET)
public Map<String, String> doIt(@PathVariable(name = "param", required = false) String param) {
Map<String, String> result = new HashMap<>();
if (param != null) {
result.put("param", param);
result.put("empty", "false");
}
else {
result.put("empty", "true");
}
return result;
}
}
This is a snippet of the spec that is generated:
paths:
/test/{param}:
get:
# ...
parameters:
- name: param
in: path
required: true
schema:
type: string
Expected behavior The path parameter should be marked as optional in the generated spec.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Spring Optional Path Variables - Baeldung
In this tutorial, we'll learn how to make a path variable optional in Spring. First, we'll describe how Spring binds @PathVariable ...
Read more >PathVariable as optional in swagger SpringFox(3.0.0)
Path parameters are always required. If we are having an optional path variable then we need to define the two separate end poinds....
Read more >Spring Boot Optional Path Variables - Yawin Tutor
Optional Path variables in spring boot can be configured using the annotation @PathVariable. if the path variable is not configured as optional and...
Read more >Spring WebMVC Optional @PathVariable - HowToDoInJava
The @PathVariable annotation allows the boolean attribute required to indicate if a path variable is mandatory or optional. Remember to store ...
Read more >Spring REST: Optional PathVariable - Roy Tutorials
In this tutorial I will show you how to define Spring optional path variable (PathVariable) in REST service. Here I will use Java...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I respectfully disagree. As I said, taking a single method that works perfectly fine, and splitting it into two in order to appease my documentation tool is artificial. There is nothing else about the structure or logic of the code that calls for two methods there. A simple null-check (if it’s even needed) is far more clear than multiple methods that are nearly-identical in signature, annotations, and behavior.
Well, I’m not expecting magic. 😄 But stylistic debates aside, I still believe there is a bug here. I don’t know the ins and outs of the OAS the way you do, but I’m fairly certain that the snippet of YAML I pasted above is not compliant with the specification of the
name
field:You can have a look at the follwing class/method if more interested: io.swagger.v3.oas.models.parameters.Parameter.setIn, from the swagger-models.
The library springdoc-openapi is built on the top of swagger-api and swagger-ui, which represents the main OpenAPI 3 implementation. There are certainly, some features permitted by spring, that may not be compliant with the OpenAPI specification. Having the possibility of path parameters to be false, is one of them. But not the only one.
As mentioned before, you can choose to make your code simpler, which will help you get the best of both worlds (spring and OpenAPI). The choice is up to you…