question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@PathVariable interpreted as a query parameter when annotated on an interface

See original GitHub issue

Versions:

springfox 2.9.2 Spring boot web starter 2.0.4

there seems to be a bug where PathVariable gets interpreted as a query parameter. It only seems to occur when using a style where @PathVariable is annotated on the interface (e.g. it works properly when annotated on the controller directly)

Given an interface:

public interface SampleApi {
 
          @RequestMapping(
              value = "/test/{var1}",
              consumes = {"application/json"},
              method = RequestMethod.PUT)
          ResponseEntity<Void> test(
              @ApiParam(value = "some named var1", required = true)
              @PathVariable("var1")
                  Long var1
              );
}

And implementation:

@Controller
public class SampleApiImpl implements SampleApi {
 
        public ResponseEntity<Void> test(Long var1) {
                return null;
        }
 
}

Yields:

"/test/{var1}": {
                        "put": {
                                "tags": ["sample-api-impl"],
                                "summary": "test",
                                "operationId": "testUsingPUT",
                                "consumes": ["application/json"],
                                "produces": ["*/*"],
                                "parameters": [{
                                        "name": "var1",
                                        "in": "query",
                                        "description": "var1",
                                        "required": false,
                                        "type": "integer",
                                        "format": "int64"
                                }],
 

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
magnus-larssoncommented, Oct 18, 2018

This seems to work now?

I tried it using springfox-swagger2 3.0.0-SNAPSHOT together with Spring Boot 2.1.0-RC1 (i.e. Spring 5.1.1-RELEASE).

An interface declaration like:

public interface ProductService {
    @GetMapping(
        value    = "/product/{productId}",
        produces = "application/json")
     Product getProduct(@PathVariable int productId);

Results in a swagger definition like:

"paths": {
    "/product/{productId}": {
      "get": {
        "tags": [
          "product-service-impl"
        ],
        "summary": "getProduct",
        "operationId": "getProductUsingGET",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "productId",
            "in": "path",
            "description": "productId",
            "required": true,
            "type": "integer",
            "format": "int32"
          }
        ],

i.e. “in” equals to “path”

An interface declaration like:

public interface RecommendationService {
    @GetMapping(
        value    = "/recommendation",
        produces = "application/json")
    List<Recommendation> getRecommendations(@RequestParam(value = "productId", required = true) int productId);

Results in a swagger definition like:

  "paths": {
    "/recommendation": {
      "get": {
        "tags": [
          "recommendation-service-impl"
        ],
        "summary": "getRecommendations",
        "operationId": "getRecommendationsUsingGET",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "productId",
            "in": "query",
            "description": "productId",
            "required": true,
            "type": "integer",
            "format": "int32"
          }
        ],

i.e. “in” equals to “query”

Or, am I missing something?

0reactions
dilipkrishcommented, Oct 23, 2018

Great that its working out of the box!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring MVC Annotated Controller Interface with @PathVariable
Apparently, when a request pattern is mapped to a method via the @RequestMapping annotation, it is mapped to to the concrete method implementation....
Read more >
Spring @RequestParam vs @PathVariable Annotations
@RequestParam and @PathVariable can both be used to extract values from the request URI, but they are a bit different.
Read more >
18.2 Creating RESTful services - Spring
The @PathVariable method parameter annotation is used to indicate that a method parameter should be bound to the value of a URI template...
Read more >
How to configure query parameters in Spring Controllers
When making a call to a controller, one can customize the behavior and the results that are produced by that endpoint by setting...
Read more >
Retrofit 2 - Query and Path parameters example
It specifies whether the argument value to the annotated method parameter is already URL encoded or not. Based on it's value, URL encoding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found