Skip to content
New issue

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

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

Already on GitHub? # to your account

Swagger-UI defaults to Body type parameter. #2111

Closed
nooruddin opened this issue Apr 21, 2016 · 3 comments
Closed

Swagger-UI defaults to Body type parameter. #2111

nooruddin opened this issue Apr 21, 2016 · 3 comments

Comments

@nooruddin
Copy link

I have following interface and equivalent implementation for a rest API.
Interface:

package com.example.sampleapi;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("authenticationStuff")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface ISampleRest {

    @Path("getMethod")
    @GET
    public Response demoGetMethod(@QueryParam("welcomeWho") final String welcomeWho);
}

Implementation:

package com.tutelatechnologies.dashboard.DashboardInterfaceEngine.server.webapi.sample;

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.TextNode;
import com.tutelatechnologies.dashboard.DashboardInterfaceEngine.server.sampleapi.ISampleRest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;

@Path("authenticationStuff")
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "/authenticationStuff", tags = "authenticationStuff")
public class SampleRest implements ISampleRest {

    final JsonNodeFactory factory = JsonNodeFactory.instance;

    @Override
    public Response demoGetMethod(@ApiParam("welcomeWho") final String welcomWho) {
        TextNode textNode = factory.textNode("called get method by: " + welcomWho);
        return getNoCacheResponseBuilder(Response.Status.OK).entity(textNode).build();
    }

    private Response.ResponseBuilder getNoCacheResponseBuilder(Response.Status status) {
        CacheControl cc = new CacheControl();
        cc.setNoCache(true);
        cc.setMaxAge(-1);
        cc.setMustRevalidate(true);

        return Response.status(status).cacheControl(cc);
    }
}

Now, When I don't specify the parameter to be a QueryParam type in implementation method, I get following output using swagger-ui and returns a 200 response with parameter value null.
capture
When I specify the parameter to be a QueryParam type in implementation, I get 404 for some issue in swagger-core reported here(swagger-api/swagger-core#1506 (comment)). However the field does show up as QueryParam type text-field in UI.
capture1

@nooruddin
Copy link
Author

Here is swagger json spec generated in both cases.
While specifying @QueryParam annotation in implementation method.

{
  "swagger": "2.0",
  "info": {
    "description": "This is Tutela API Dashboard.  Find out More about Tutela Technologies Ltd. at [http://www.tutelatechnologies.com/](http://www.tutelatechnologies.com/)",
    "version": "1.2.0",
    "title": "Tutela API Documentation",
    "contact": {
      "name": "TUTELA TECHNOLOGIES LTD."
    },
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "basePath": "/rest",
  "tags": [
    {
      "name": "authenticationStuff"
    }
  ],
  "schemes": [
    "http",
    "https"
  ],
  "paths": {
    "/authenticationStuff/getMethod": {
      "get": {
        "tags": [
          "authenticationStuff"
        ],
        "operationId": "demoGetMethod",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "welcomeWho",
            "in": "query",
            "description": "welcomeWho",
            "required": false,
            "type": "string"
          }
        ],
        "responses": {
          "default": {
            "description": "successful operation"
          }
        }
      }
    }
  },
  "securityDefinitions": {
    "cwak_key": {
      "type": "apiKey",
      "name": "cwak_key",
      "in": "header"
    }
  },
  "definitions": {    
  }
}

When I don't specify QueryParam annotation in implementation method.

{
  "swagger": "2.0",
  "info": {
    "description": "This is Tutela API Dashboard.  Find out More about Tutela Technologies Ltd. at [http://www.tutelatechnologies.com/](http://www.tutelatechnologies.com/)",
    "version": "1.2.0",
    "title": "Tutela API Documentation",
    "contact": {
      "name": "TUTELA TECHNOLOGIES LTD."
    },
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "basePath": "/rest",
  "tags": [
    {
      "name": "authenticationStuff"
    }
  ],
  "schemes": [
    "http",
    "https"
  ],
  "paths": {
    "/authenticationStuff/getMethod": {
      "get": {
        "tags": [
          "authenticationStuff"
        ],
        "operationId": "demoGetMethod",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "description": "welcomeWho",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "default": {
            "description": "successful operation"
          }
        }
      }
    }
  },
  "securityDefinitions": {
    "cwak_key": {
      "type": "apiKey",
      "name": "cwak_key",
      "in": "header"
    }
  },
  "definitions": {
  }
}

@webron
Copy link
Contributor

webron commented Apr 22, 2016

As you already figured out, the issue is with swagger-core and should be handled there. There are two PRs that may resolve the issue, looking into merging them soon.

@webron webron closed this as completed Apr 22, 2016
@nooruddin
Copy link
Author

Thanks for update

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

No branches or pull requests

3 participants