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

Add support for Path Item and Operation servers (v3) #1669

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

krokettenkoal
Copy link
Contributor

Currently, servers defined inside Path Items and Operations in OpenAPI3 specifications are ignored, and the (first) server specified in the OpenAPI Object is used for every request. This may break clients for APIs using different service endpoints on path-level or operation-level.

Example

The Google Maps Platform OpenAPI3 Specification defines different service endpoints (servers) for some APIs (paths), with the base server being https://www.googleapis.com. For example, the Places API is using https://maps.googleapis.com as an endpoint, while the Roads API is using https://roads.googleapis.com, and the Geolocation API is using https://www.googleapis.com.

When generating a client from the provided OpenAPI3 specification (YAML or JSON), the resulting services and their operations always send their requests to the base server URL https://www.googleapis.com, breaking all APIs that use servers other than the base server (in this case: most of them).

Solution

Path-level and operation-level servers are already being parsed from v3 specifications. However, their values are not used when exporting (generating) services and operations. This PR addresses this issue by including an optional server property in the Service and Operation interfaces, which is set by the parser to the first server's URL in the corresponding Path Item Object or Operation Object (if available). Additionally, the ApiRequestOptions type now includes an optional server property as well, which is set to the according Service's or Operation's server URL (preferring operation-level servers to path-level servers) when making a request. The provided server is then considered in the client's getUrl() function, falling back to the base server if the request options do not contain a server URL (or the URL is empty).

Remarks

I have originally seen this functionality as a new feature. The more I think about it, however, the more it seems like a bugfix to me, as it is described in the OpenAPI3 specs and may cause broken services and/or operations when missing. It does not appear to be too critical, though.

Services and operations generated from OpenAPI v3 specifications now respect servers defined in Path Items (see https://spec.openapis.org/oas/v3.1.0#path-item-object) and Operations (see https://spec.openapis.org/oas/v3.1.0#operation-object). This implies the following changes:
* Service.d.ts and Operation.d.ts now include an optional `server` property, being set to the first server's url of the `servers` property in their corresponding Path Item Object or Operation Object in the spec. The behavior is the same as with the `OpenAPIConfig.BASE` property.
* `ApiRequestOptions` now includes an optional `server` property, providing any path-level or operation-level server to requests. If specified, the provided value is used as base url instead of the `OpenAPIConfig.BASE` value in `getUrl()`.
* Test cases now cover path-level or operation-level servers.
* Test snapshots have been updated accordingly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant