Skip to content

Custom Converters are not excluded if not registered for Http Message Converter #2165

Closed
@RittikeGhosh

Description

@RittikeGhosh

Bug Description
Feature to support Custom Spring Converter(#1534), is generating wrong openapi specification. I think all converters are considered as Http Message Converter, which it shouldn't. Hence it is generating the request body schema as the Source Object picked from the converter with target as the actual Object defined in the Request Handler Method in Controller.
This is happening for both WebMvc and WebFlux.

To Reproduce
Using :

  • spring boot 3.0.5
  • webflux
  • org.springdoc:springdoc-openapi-starter-webflux-ui:2.0.4

Code setup:

record ObjectA() {};

record ObjectB() {};

@Component
class BToAConvertor implements Converter<ObjectB, ObjectA> {
    @Override
    public ObjectA convert(ObjectB source) {
        return new ObjectA();
    }
}

@RestController
class Controller {
    @GetMapping("/test")
    public String test(@RequestBody ObjectA request) {
        return "OK!";
    }
}

Output OpenApi Specification:
Generated ObjectB instead of ObjectA as request schema

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: http://localhost:8080
    description: Generated server url
paths:
  /test:
    get:
      tags:
        - controller
      operationId: test
      parameters:
        - name: request
          in: query
          required: true
          schema:
            $ref: '#/components/schemas/ObjectB'
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                type: string
components:
  schemas:
    ObjectB:
      type: object

Expected behavior
I expect the converters to be picked only if it is registered for Http Message Converter. Here I didn't register it for that, it is used for other internal purpose.

Expected OpenApi Specification:

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: http://localhost:8080
    description: Generated server url
paths:
  /test:
    get:
      tags:
        - controller
      operationId: test
      parameters:
        - name: request
          in: query
          required: true
          schema:
            $ref: '#/components/schemas/ObjectA'
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                type: string
components:
  schemas:
    ObjectA:
      type: object

Workaround Code

@GetMapping(value = "/test")
@Operation(requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
        required = true,
        content = @Content(schema = @Schema(implementation = ObjectA.class)))
)
public String test(@RequestBody ObjectA request) {
    return "OK!";
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions