4

I am getting validation errors in my Springdoc generated OpenAPI spec and can't see to find an example online that matches how my Java code is formed.

I am trying to generate an OpenAPI spec with Springdoc for a Spring Boot controller. I have a mapping for a path that has multiple path variables and the method signature accepts a command object (the command object is automatically constructed from these path variables). Swagger-UI.html seems to work more or less but the generated JSON/YAML spec does not appear to be valid.

Code snippet I'm referring to:

@GetMapping("/myPath/{foo}/{bar}/{baz}")
public Mono<MyServiceResponse> doSomethingInteresting(@Valid DoSomethingInterestingCommand command) {
    return interestingService.doSomethingInteresting(command);
}

The OpenApi snippet this generates is:

paths:
  '/myPath/{foo}/{bar}/{baz}':
    get:
      tags:
        - my-controller
      operationId: doSomethingInteresting
      parameters:
        - name: command
          in: query
          required: true
          schema:
            $ref: '#/components/schemas/DoSomethingInterestingCommand'

This then yields errors such as this:

Semantic error at paths./myPath/{foo}/{bar}/{baz}
Declared path parameter "foo" needs to be defined as a path parameter at either the path or operation level

What should I do differently in order to get the generated spec to be well-formed? I am also curious as to why the swagger-ui.html page seems to be working OK, but that is less critical.

1

2 Answers 2

8

To generate the correct openAPI spec, you can add the swagger-annotations.

@Parameter(in = ParameterIn.PATH, name ="foo" ,schema = @Schema(type = "string"))
@Parameter(in = ParameterIn.PATH, name ="bar" ,schema = @Schema(type = "string"))
@Parameter(in = ParameterIn.PATH, name ="baz" ,schema = @Schema(type = "string"))
@GetMapping("/myPath/{foo}/{bar}/{baz}")
public Mono<MyServiceResponse> doSomethingInteresting(@Valid @Parameter(hidden = true) DoSomethingInterestingCommand command) {
    return interestingService.doSomethingInteresting(command);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I needed - thanks!
-1

Pay attention please, that @Parameter is not shown in UI with POST. Because There is only body in POST. So you should use either @io.swagger.v3.oas.annotations.parameters.RequestBody(description="blabla") and @org.springframework.web.bind.annotation.RequestBody

2 Comments

Please provide an explanation to your code - it is very hard to understand something when it isn't explained.
This is not an answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.