Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@GetMapping and @PathVariable: not detected during parsing #1023

Closed
joao64 opened this issue May 11, 2020 · 5 comments
Closed

@GetMapping and @PathVariable: not detected during parsing #1023

joao64 opened this issue May 11, 2020 · 5 comments
Milestone

Comments

@joao64
Copy link

joao64 commented May 11, 2020

Hi,

using these SpringWeb Framework annotation, the enunciate output format doesn't catch well the path variable arguments.

An example class using @GetMapping (same also with @PostMapping):

@RestController
@RequestMapping(
        value = {"/rest/basic"},
        produces = "application/json"
)
public class BasicRest {

    @GetMapping("/echo/{text}")
    public BasicResponse echo(
            @PathVariable("text") String text
    ) {
        return new BasicResponse(text);
    }
}

And the core enunciate.xml file:

<enunciate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://enunciate.webcohesion.com/schemas/enunciate-2.13.0.xsd">

    
    <!-- <application path=""  root="http://localhost:8080/system-ui" /> -->
    <application path=""  root="http://10.201.83.16/svil" />
    
    <modules>
        <!-- provider -->

        <spring-web  groupBy="class" />
        <jaxrs disabled="true" />
        <jaxws disabled="true" />

        <!-- not provider -->
        
        <docs disabled="false" docsSubdir="apidocs" includeApplicationPath="false"/>
        <swagger disabled="false" docsSubdir="swaggerui/swagger-ui-dist" base="src/main/resources/swagger-ui-dist" f/>


</enunciate>

Eventually, the generated swagger.json model regarding that endpoint is (the PathVariable parameter text is missing):

"/rest/basic/echo/{text}" : {  
    "get" : {  
    "tags" : [ "BasicRest" ],
        "operationId" : "version",
        "produces" : [ "application/json" ],
        "parameters" : [
        ],
        "responses" : {
          "200" : {
            "schema" : {
"$ref" : "#/definitions/BasicResponse",
"description" : ""
            },
            "examples" : {
              "application/json" : {
  "payload" : "..."
}
            },
            "description" : ""
          }
        },
		"security": [
			{
				"bearerAuth":[]
			}
		]
      },
@stoicflame stoicflame added the bug label May 11, 2020
@stoicflame stoicflame added this to the 2.14.0 milestone May 11, 2020
@stoicflame stoicflame added support and removed bug labels May 11, 2020
@stoicflame
Copy link
Owner

Thanks for the report.

Unfortunately, I'm not able to reproduce the problem. I applied these changes to the project:

diff --git a/examples/spring-petclinic/src/main/java/org/springframework/samples/petclinic/web/TestRestController.java b/examples/spring-petclinic/src/main/java/org/springframework/samples/petclinic/web/TestRestController.java
index 5dfdbe19e..3db050b75 100644
--- a/examples/spring-petclinic/src/main/java/org/springframework/samples/petclinic/web/TestRestController.java
+++ b/examples/spring-petclinic/src/main/java/org/springframework/samples/petclinic/web/TestRestController.java
@@ -3,10 +3,8 @@ package org.springframework.samples.petclinic.web;
 import java.util.Map;
 
 import org.springframework.http.MediaType;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.samples.petclinic.model.Vets;
+import org.springframework.web.bind.annotation.*;
 
 @RestController
 @RequestMapping("/test")
@@ -19,4 +17,15 @@ public class TestRestController {
   Map<String, String> testDoc(@RequestBody Map<String, String> testBody) {
     return testBody;
   }
+
+  /**
+   * Echo something.
+   *
+   * @param text the text to echo.
+   * @return the text.
+   */
+  @GetMapping("/echo/{text}")
+  public String echo(@PathVariable("text") String text) {
+    return text;
+  }
 }
\ No newline at end of file

And both the documentation and resulting swagger.json included the path parameter:

    "\/test/echo/{text}" : {
      "get" : {
        "tags" : [ "TestRestController" ],
        "description" : "Echo something.",
        "operationId" : "echo",
        "produces" : [ "application/json", "application/xml" ],
        "parameters" : [
          ...,
          {
            "name" : "text",
            "in" : "path",
            "required" : true,
            "type" : "string",
            "description" : "the text to echo."
          }
        ],
        ...
      },

So in order to debug this, I'm going to need more details on how to reproduce the problem. If you can provide a patch that exposes it, that would be great.

@joao64
Copy link
Author

joao64 commented May 17, 2020

Hi, thanks for your initial investigation. While trying to reproduce the problem of the missing path variable (which unfortunately I cannot reproduce until now with your test code) I noted another wrong behaviour.

It assign to each mapping all the operations. So instead of having only 2 definitions, we have 4.

Starting from this code:

package org.springframework.samples.petclinic.web;

import java.security.Principal;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;

@RestController
@RequestMapping(
        value="/test",
        produces="application/json")
public class TestRestController {

  
    @GetMapping("/echo/{text}")
    public String echo(
            @PathVariable("text") String text,
            @RequestHeader(value="somestring", required=false) String ctf
    ) {
        return text;
    }
    
    @DeleteMapping("/widgets/{widgetId}")
    public void deleteWidget(
            Principal principal,
            @PathVariable("widgetId") Long widgetId
    )   {
        // do nothing
    }

}`


I got this swagger.json:

```json
 "\/test/echo/{text}" : {
      "delete" : {
        "tags" : [ "TestRestController" ],
        "operationId" : "deleteWidget",
        "produces" : [ "application/json" ],
        "parameters" : [
         ...
          {
            "name" : "widgetId",
            "in" : "path",
            "required" : true,
            "type" : "number",
            "description" : ""
          }
        ],
        "responses" : {
          "204" : {
            "schema" : {
"description" : "",
"type" : "file"
            },
            "description" : ""
          }
        }
      },
 "get" : {
        "tags" : [ "TestRestController" ],
        "operationId" : "echo",
        "produces" : [ "application/json" ],
        "parameters" : [
       ...
          {
            "name" : "somestring",
            "in" : "header",
            "type" : "string",
            "description" : ""
          },
          {
            "name" : "text",
            "in" : "path",
            "required" : true,
            "type" : "string",
            "description" : ""
          }
        ],
        "responses" : {
          "200" : {
            "schema" : {
"description" : "",
"type" : "string"
            },
            "description" : ""
          }
        }
      }
    },
 "\/test/widgets/{widgetId}" : {
      "delete" : {
        "tags" : [ "TestRestController" ],
        "operationId" : "deleteWidget",
        "produces" : [ "application/json" ],
        "parameters" : [
        ...
          {
            "name" : "widgetId",
            "in" : "path",
            "required" : true,
            "type" : "number",
            "description" : ""
          }
        ],
        "responses" : {
          "204" : {
            "schema" : {
"description" : "",
"type" : "file"
            },
            "description" : ""
          }
        }
      },
 "get" : {
        "tags" : [ "TestRestController" ],
        "operationId" : "echo",
        "produces" : [ "application/json" ],
        "parameters" : [
        ...
          {
            "name" : "somestring",
            "in" : "header",
            "type" : "string",
            "description" : ""
          },
          {
            "name" : "text",
            "in" : "path",
            "required" : true,
            "type" : "string",
            "description" : ""
          }
        ],
        "responses" : {
          "200" : {
            "schema" : {
"description" : "",
"type" : "string"
            },
            "description" : ""
          }
        }
      }
    },

@stoicflame
Copy link
Owner

Thanks for the report. Fixed at 00cd32a.

If you can provide me a way to reproduce the original reported issue, let me know and I'll re-open.

@stoicflame
Copy link
Owner

Fix is available in 2.13.1.

@joao64
Copy link
Author

joao64 commented May 26, 2020

Fix is available in 2.13.1.

Thanks, appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants