Pavel Petrashov
Pavel Petrashov

Reputation: 1262

springdoc-openapi-ui + swagger don't understand @PathVariable required = false flag

I use this library for generation documentation:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.5.0</version>
</dependency>

I have this controller:

@RestController
public class TestController {

    @GetMapping("/test{hz}")
    public String test(@PathVariable(value = "hz", required = false) String hz) {
        return "test";
    }
}

But I get this documentation:

enter image description here

Why required = false doesn't work?

I tried this:

@RestController
public class TestController {

    @GetMapping("/test{hz}")
    public String test(
            @Parameter(description = "foo", required = false)
            @PathVariable(value = "hz", required = false) String hz) {
        return "test";
    }
}

It doesn't work too

EDIT: (Answer for @Helen comment) - Of course I know about this:

@RestController
public class TestController {

    @GetMapping(value = {"/test", "/test{hz}"})
    public String test(
            @Parameter(description = "foo", required = false)
            @PathVariable(value = "hz", required = false) String hz) {
        return "test";
    }
}

And I tried this:

@PathVariable(value = "hz", required = false) Optional<String> hz

It makes documentation worse. so I didn't add this code. With {"/test", "/test{hz}"} It looks like this:

enter image description here

Upvotes: 8

Views: 6714

Answers (1)

brianbro
brianbro

Reputation: 4789

This is conform to the OpenAPI specification.

Each path parameter must be substituted with an actual value when the client makes an API call. In OpenAPI, a path parameter is defined using in: path. The parameter name must be the same as specified in the path. Also remember to add required: true, because path parameters are always required.

You can have a look at the documentation:

Upvotes: 0

Related Questions