Skip to content

Server Base Path Not Applied to Generated Endpoint URLs #112

@bowenwr

Description

@bowenwr

Describe the bug
When specifying a url in servers in a spec, the path is completely ignored in the generated endpoints. Making it necessary to fully qualify the base_url passed to the Client, which is problematic when supporting multiple API namespaces (e.g., /api/v2, /api/experimental).

To Reproduce
Steps to reproduce the behavior:

  1. Given a spec file with a relative url defined in servers
  2. Generate a client: openapi-python-client generate --path openapi.json
  3. Use the generated client to make a request:
from example_api.client import Client
from example_api.api.requests import get_request

client = Client(base_url="https://example.com/")
response = get_request(client=self.client, request_id="request_id")

The generated code in requests shows:

url = "{}/requests/{request_id}".format(client.base_url, request_id=request_id)

The first part of the URL only puts in the server name passed to Client, but ignores the specified server path from the spec. Resulting in an invalid URL like:

GET https://example.com/requests/request_id

Expected behavior

Should generate an HTTP request like:

GET https://example.com/api/v2/requests/request_id

The generated code would write the base path in like:

url = "{}/api/v2/requests/{request_id}".format(client.base_url, request_id=request_id)

OpenAPI Spec File

{
	"openapi": "3.0.1",
	"info": {
		"title": "Example API",
		"version": "2.0.0"
	},
	"servers": [{
		"url": "/api/v2"
	}],
	"paths": {
		"/requests/{request_id}": {
			"get": {
				"tags": [
					"requests"
				],
				"description": "Get a request by ID",
				"operationId": "getRequest",
				"parameters": [{
					"name": "request_id",
					"in": "path",
					"schema": {
						"type": "string"
					},
					"required": true
				}],
				"responses": {
					"200": {
						"description": "OK",
						"content": {
							"application/json": {
								"schema": {
									"$ref": "#/components/schemas/Request"
								}
							}
						}
					}
				}
			}
		}
	},
	"components": {
		"schemas": {
			"Request": {
				"type": "object",
				"properties": {
					"id": {
						"type": "string"
					}
				}
			}
		}
	}
}

This change might need to be behind a compatibility flag to avoid breaking existing code.

Desktop (please complete the following information):

  • OS: macOS 10.15.2
  • Python Version: 3.8.0
  • openapi-python-client version: 0.4.2

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions