springdoc-openapi icon indicating copy to clipboard operation
springdoc-openapi copied to clipboard

Path variable mapped improperly in swagger-ui

Open matios13 opened this issue 3 years ago • 9 comments

Describe the bug

I have simple controller in Kotlin

    @Operation(summary = "Get company details", description = "Get company details")
    fun getCompanyDetails(@PathVariable companyId: Long): ResponseEntity<CompanyDTO> {
        val company = companyService.getCompanyByIdOrException(companyId)
        return ResponseEntity.ok(company.toDto())
    }

After upgrading to Spring-boot 3.0.1 and springdoc-openapi 2.0.2 I see arg0 in swagger ui rather than companyId in the ui

image

And when trying it out i get 404:

  "message": "Failed to convert value of type 'java.lang.String' to required type 'long'; For input string: \"{companyId}\"",
  "path": "/api/evans-admin/companies/%7BcompanyId%7D/scheme-admins"

as you can see rather than using given value %7BcompanyId%7D string is being used

matios13 avatar Jan 02 '23 10:01 matios13

I'm seeing the same issue, for both PathVariable and RequestParam.

singhbaljit avatar Jan 05 '23 05:01 singhbaljit

This actually looks like an issue with Spring Framework itself. I'm seeing arg0, etc. in Spring logs (i.e. https://github.com/spring-projects/spring-framework/issues/29776). Using the -parameters compiler flag fixed the problem.

singhbaljit avatar Jan 05 '23 20:01 singhbaljit

It seems that the -parameters option does not solve this problem in Gradle.

  • Environment: Spring boot: 3.0.0, springdoc-openapi (starter webmvc ui): 2.0.2

I set build.gradle as follows.

plugins {
    id 'org.springframework.boot' version '3.0.0'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}

...
  
tasks.withType(JavaCompile) {
    options.compilerArgs.add("-parameters")
}

Instead, the easy way to solve this problem is to clearly define the value (or name) of the @PathVariable annotation.

    @Operation(summary = "Get company details", description = "Get company details")
    fun getCompanyDetails(@PathVariable(value = "companyId") companyId: Long): ResponseEntity<CompanyDTO> {
        val company = companyService.getCompanyByIdOrException(companyId)
        return ResponseEntity.ok(company.toDto())
    }

In my application, there was the same issue when MutlipartFile was received as @RequestParam, and in this case, it was solved by clearly writing annoation value (or name).

There seems to be a problem that the current library cannot read this value.

KimDoubleB avatar Jan 15 '23 04:01 KimDoubleB

I guess this is the same issue as https://github.com/springdoc/springdoc-openapi/issues/2036

vojkny avatar Jan 18 '23 10:01 vojkny

@matios13,

You might need to use -parameters. I am adding an enhancement. Please do not hesitate to share if this fixes the issue.

bnasslahsen avatar Feb 05 '23 00:02 bnasslahsen

Can you 🙏 release a new version with this fix? Can you specify details of You might need to use -parameters? This worked correctly until now with no -parameters field.

vojkny avatar Feb 16 '23 10:02 vojkny

@knyttl,

The release would be at the latest, by the end of this month. You can already do your tests with the latest SNAPSHOT. The explanation of the might need to use -parameters, is related to the deprecation of LocalVariableTableParameterNameDiscoverer with spring-boot 3:

  • https://github.com/spring-projects/spring-framework/blob/main/spring-core/src/main/java/org/springframework/core/LocalVariableTableParameterNameDiscoverer.java

And in the new Default implementation is now:

  • https://github.com/spring-projects/spring-framework/blob/main/spring-core/src/main/java/org/springframework/core/DefaultParameterNameDiscoverer.java

bnasslahsen avatar Feb 16 '23 11:02 bnasslahsen

Hi, would you be able to release earlier? The sonatype snapshot repo is down today (returns 504) and we cannot build at all :/

vojkny avatar Feb 20 '23 16:02 vojkny

@bnasslahsen In latest version it is still required to use -parameters compiler flag for proper mapping. It is also mentioned in docs.

Is this correct behavior?

lukago avatar Aug 13 '24 10:08 lukago