question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional path variables are marked as required

See original GitHub issue

Hello,

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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
macdaddyazcommented, Apr 6, 2020

You workaround is better, and adds more clarity to what you want achieve.

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.

There is no magic for documentation generation.

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:

If in is “path”, the name field MUST correspond to a template expression occurring within the path field in the Paths Object. See Path Templating for further information.

1reaction
bnasslahsencommented, Apr 6, 2020

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.

    public void setIn(String in) {
        if ("path".equals(in)) {
            this.required = true;
        }

        this.in = in;
    }

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…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found