Server Base Path Not Applied to Generated Endpoint URLs #833
Replies: 5 comments
-
It will take some consideration add in support for servers, though maybe we can add in partial support in the near term without building out the full set. Here are some things OpenAPI servers are supposed to be able to do:
There are also some usability questions like:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the response! I definitely appreciate that the servers block could be tricky to fully support. I like the approach/convention of auto-selecting the first server if only one is defined. It seems like the most sane, common behavior. The client might also be able to store all the generated servers and allow them as optional parameter for a given call. So a sample generated method signature: Given: class Client:
base_url: str
servers: Optional[List[Server]]
def default_server(self) -> Optional[Server]:
if servers:
return servers[0]
return None Sample generated method: def get_request(*, client: Client, request_id: str, server: Server = client.default_server()) -> Request: If server is |
Beta Was this translation helpful? Give feedback.
-
We might also add @dataclass
class ServerVariable:
""" A representation of the OpenAPI Server Variable Object"""
default: str
description: Optional[str]
enum: Optional[List[str]]
@dataclass
class Server:
""" A representation of the OpenAPI Server Object"""
url: str
description: Optional[str]
variables: Optional[Dict[str, ServerVariable]]
def resolve_url(self, variable_values: Optional[Dict[str, str]]) -> str:
substitutions = self._default_values().copy()
substitutions.update(variable_values)
return self.url.format(**substitutions)
def _default_values(self) -> Dict[str, str]:
return {
key: variable.default
for key, variable in self.variables.items()
} The endpoint internally would call the specified server's |
Beta Was this translation helpful? Give feedback.
-
Just ran into this, probably a hard blocker for our usage. |
Beta Was this translation helpful? Give feedback.
-
Anything new regarding this issue? Any workarounds? |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
When specifying a
url
inservers
in a spec, the path is completely ignored in the generated endpoints. Making it necessary to fully qualify thebase_url
passed to theClient
, which is problematic when supporting multiple API namespaces (e.g.,/api/v2
,/api/experimental
).To Reproduce
Steps to reproduce the behavior:
url
defined inservers
openapi-python-client generate --path openapi.json
The generated code in
requests
shows: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:Expected behavior
Should generate an HTTP request like:
The generated code would write the base path in like:
OpenAPI Spec File
This change might need to be behind a compatibility flag to avoid breaking existing code.
Desktop (please complete the following information):
Additional context
Add any other context about the problem here.
Beta Was this translation helpful? Give feedback.
All reactions