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

How to define application name for try it curl requests? #1081

Closed
rautars opened this issue Jun 10, 2017 · 10 comments
Closed

How to define application name for try it curl requests? #1081

rautars opened this issue Jun 10, 2017 · 10 comments

Comments

@rautars
Copy link

rautars commented Jun 10, 2017

I cannot find anywhere how to define application name for try it curl requests.

My Swagger page is at url: http://localhost:8080/my-app/swagger.xhtml (it is based on pet store sample)
Services base url is /api/notes/v1/
When I hit execute at try it curl hits service with url http://localhost:8080/api/notes/v1/mynotes since there no 'my-app' in path included I get 404.

Application name varies from environment and I cannot simple put in swagger.json because same spec will be used for generating static docs (application name is not relevant there).

SwaggerUIBundle looks currently this way:

	const ui = SwaggerUIBundle({
		url : "/my-app/api/notes/v1/mynotes/swagger.json",
		dom_id : '#swagger-ui',
		presets : [ 
			SwaggerUIBundle.presets.apis,
			SwaggerUIStandalonePreset
		],
		plugins : [
			SwaggerUIBundle.plugins.DownloadUrl
		],
		validatorUrl : null,
		layout : "StandaloneLayout"
	})

I'm using swagger-ui 3.0.10 from WebJar.

@webron
Copy link
Contributor

webron commented Jun 12, 2017

First - this is a swagger-ui question, not swagger-js.

Second - it's likely that there's something wrong with your API definition, can you share it?

@rautars
Copy link
Author

rautars commented Jun 13, 2017

Here API swagger.json:

{
  "swagger": "2.0",
  "info": {
    "title": "Notes Services",
    "version": "1",
  },
  "basePath": "/api/notes/v1",
  "tags": [
    {
      "name": "notes"
    }
  ],
  "schemes": [
    "http",
    "https"
  ],
  "paths": {
    "/mynotes": {
      "get": {
        "tags": [
          "notes"
        ],
        "summary": "Gets my notes",
        "description": "",
        "operationId": "retrieveMyNotes",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "priority",
            "in": "query",
            "description": "Priority",
            "required": false,
            "type": "integer",
            "x-example": "1"
          }
        ],
        "responses": {
          "200": {
            "description": "successful operation",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/NoteDto"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "NoteDto": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "priority": {
          "type": "integer",
          "format": "int32"
        },
        "text": {
          "type": "string"
        }
      }
    }
  }
}

@webron
Copy link
Contributor

webron commented Jun 13, 2017

Your basePath is /api/notes/v1 - it should be /my-app/api/notes/v1/ in order to work properly.

@rautars
Copy link
Author

rautars commented Jun 13, 2017

The problem is that context path 'my-app' is part of server container configuration which I cannot control (not part of application code). Another thing we also generate static html documentation based on same swagger.json files and context path 'my-app' in front of base path would be incorrect from clients standpoint.

The closest pending feature I found which could help in current situation is #1045. However I'm not sure if http://localhost:8080/my-app swagger would interpret it as valid host for try-it functionality it I would override it at runtime.

@webron
Copy link
Contributor

webron commented Jun 13, 2017

The host cannot contain paths as well. You need to modify your basePath. Since it's generated from your code, it can be generated dynamically by the code. You don't need to control it. If you don't change it, it will break the UI, will break code generation, and pretty much make your API definition invalid.

@rautars
Copy link
Author

rautars commented Jun 14, 2017

If you don't change it, it will break the UI, will break code generation, and pretty much make your API definition invalid.

The only thing which does not work is 'try-it' due execution URL. Generation works fine I don't need put context path into generated docs as it does not make sense, clients on other end will have other context path anyway.

I just want clarify that context path is not part of REST specification its just application entry point.

In my situation url structure is: http://host:port/context/services/.
http://host:port/context - is application entry point
http://host:port/context/services/ - REST services entry point where basePath would be services

Seems swagger assumes that application entry always is http://host:port which is not always the case.

@webron
Copy link
Contributor

webron commented Jun 14, 2017

Swagger doesn't assume anything. This is how the spec works.

host - the address/ip + port of your server.
basePath - anything that comes after until the root of your API calls.

If you don't change your basePath, you don't have a definition that describes your API as the spec requires it. Either change it, or it won't work.

@rautars
Copy link
Author

rautars commented Jun 14, 2017

Thanks for clarifying

@rautars rautars closed this as completed Jun 14, 2017
@Le-Zhang
Copy link

How this issue resolved? It doesn't work when I try to change basePath. Take the example from above, simply modify basePath from /api/notes/v1 to my-app/api/notes/v1 is not working. Because after that modification, the correct API changes to http://host:port/my-app/my-app/api/notes/v1.... But it tries to make request to http://host:port/my-app/api/notes/v1... when click try it button

@webron
Copy link
Contributor

webron commented Apr 21, 2018

@Le-Zhang can't do much with that without knowing which version of swagger-ui you're using and other things. Please file a new ticket following the issue template and we'll try to help.

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

3 participants