springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.29k stars 499 forks source link

Url parameters not using provided value in Swagger UI #2724

Closed akuchcik closed 1 month ago

akuchcik commented 1 month ago

To Reproduce Issue occurs when using springdoc-openapi-starter-webmvc-ui with custom yaml file Using the same file with docker image swaggerapi/swagger-ui doesn't result with this issue and works as expected

bnasslahsen commented 1 month ago

Not reproducible. Feel free to provide a Minimal, Reproducible Example - with HelloController that reproduces the problem.

This ticket will be closed, but can be reopened if your provide the reproducible sample.

akuchcik commented 1 month ago

I found out that the reason for this is the "=" (equals sign) in the path. This path works: /data/logging-status/broker/{broker-identifier}: but this path doesn't work: /data/logging-status/broker={broker-identifier}:

Is this enough to reproduce the problem? I uploaded my file as .txt because github doesn't support .yaml files. test.txt

akuchcik commented 1 month ago

@bnasslahsen could you please look into this again? Thank you.

bnasslahsen commented 1 month ago

@akuchcik,

Try providing the github link to a Minimal, Reproducible Example - with HelloController that reproduces the problem.

akuchcik commented 1 month ago

@bnasslahsen to reproduce you have to use a custom yaml file in your application which should be available in the class path like this:

@Path("/openapi")
public class OpenApiDocService {
    @GET
    @Path("/test")
    @Produces("text/yaml")
    public Response getOpenApiFile() throws IOException {
        String filePath = "openapi.yaml";
        ClassPathResource classPathResource = new ClassPathResource(filePath);

        if (classPathResource.exists()) {
            InputStream inputStream = classPathResource.getInputStream();
            // Return the YAML content directly
            return Response.ok(StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8)).build();
        } else {
            // File not found, return HTTP 404 Not Found
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    }
}

with a custom swagger config which will be included in a spring boot application like this:

@Configuration
public class CustomSwaggerConfig {

    @Bean
    public SwaggerWelcomeWebMvc swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig,
                                               SpringDocConfigProperties springDocConfigProperties,
                                               SwaggerUiConfigParameters swaggerUiConfigParameters,
                                               SpringWebProvider springWebProvider) {
        swaggerUiConfig.setUrl("/openapi/test");
        swaggerUiConfigParameters.setShowExtensions(true);
        return new SwaggerWelcomeWebMvc(swaggerUiConfig, springDocConfigProperties,
                swaggerUiConfigParameters, springWebProvider);
    }
}

here is the yaml file and I quote my previous comment: "I found out that the reason for this is the "=" (equals sign) in the path. This path works: /data/logging-status/broker/{broker-identifier}: but this path doesn't work as the provided parameter is ignored as you can see in the picture that I provided earlier: /data/logging-status/broker={broker-identifier}:":

openapi: 3.0.2
info:
  title: Swagger Petstore - OpenAPI 3.0
  version: 1.0.19
servers:
- url: /rests
paths:
  /pet/{petId}:
    get:
      tags:
        - pet
      parameters:
        - name: petId
          in: path
          description: ID of pet to return
          required: true
          schema:
            type: string
      responses:
        "200":
          description: successful operation
          content:
            application/xml:
              schema:
                $ref: '#/components/schemas/Pet'
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        "400":
          description: Invalid ID supplied
        "404":
          description: Pet not found
  "/data/logging-status/broker={broker-identifier}":
    get:
      tags:
        - logging
      parameters:
        - name: broker-identifier
          in: path
          description: Id of broker
          required: true
          schema:
            type: string
      responses:
        "200":
          description: successful operation
          content:
            application/xml:
              schema:
                $ref: '#/components/schemas/Pet'
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        "400":
          description: Invalid ID supplied
        "404":
          description: Pet not found
components:
  schemas:
    Category:
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 1
        name:
          type: string
          example: Dogs
      xml:
        name: category
    Tag:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
      xml:
        name: tag
    Pet:
      required:
        - name
        - photoUrls
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 10
        name:
          type: string
          example: doggie
        category:
          $ref: '#/components/schemas/Category'
        photoUrls:
          type: array
          xml:
            wrapped: true
          items:
            type: string
            xml:
              name: photoUrl
        tags:
          type: array
          xml:
            wrapped: true
          items:
            $ref: '#/components/schemas/Tag'
        status:
          type: string
          description: pet status in the store
          enum:
            - available
            - pending
            - sold
      xml:
        name: pet
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic
bnasslahsen commented 1 month ago

It seems that your controllers do not follow typical Spring coding conventions. As mentioned earlier, could you please upload all of your code to a public GitHub repository for further review? Otherwise, we may need to pause this conversation.

akuchcik commented 1 month ago

I created a new spring boot application with minimal requirements https://github.com/akuchcik/springdoc-url-parameter-bug :

akuchcik commented 1 month ago

@bnasslahsen I hope this is enough to replicate this issue.

bnasslahsen commented 1 month ago

@Jelicho,

Your expected behavior is invalid. You are declaringbroker-identifier as path variable, whereas it should be declared as query parameter.

NOTE: OpenAPI defines a unique operation as a combination of a path and an HTTP method:

akuchcik commented 1 month ago

@bnasslahsen if I define it as query the resulted url looks like this: "http://localhost:8080/data/logging-status/broker={broker-identifier}?broker-identifier=test" image

It just add a query at the end of the url. I am not using a query as I'm not using "?" after the path. I am specifying an entry in a list.

akuchcik commented 1 month ago

This describing of entries/keys in a list of elements is defined in RESTCONF Protocol - RFC 8040. When using docker image swaggerapi/swagger-ui it is working as expected.

akuchcik commented 1 month ago

@bnasslahsen Did you get a chance to look at my last comments?