python-openapi / openapi-core

Openapi-core is a Python library that adds client-side and server-side support for the OpenAPI v3.0 and OpenAPI v3.1 specification.
BSD 3-Clause "New" or "Revised" License
299 stars 132 forks source link

[Feature]: Auto-detect `base_uri` when using `OpenAPI.from_file_path` #801

Closed jonathanberthias closed 3 months ago

jonathanberthias commented 7 months ago

Suggested Behavior

When I create an OpenAPI instance with a multi-file schema, I find myself writing:

spec_path = "path/to/spec.yaml"
openapi = openapi_core.OpenAPI.from_file_path(
    spec_path,
    config=openapi_core.Config(spec_base_uri=Path(spec_path).as_uri()),
)

This feels redundant as the implementation for SchemaPath.from_file_path also loads the base_uri, but that information is then lost: https://github.com/p1c2u/jsonschema-path/blob/f35bd22aa5a0b86fa4c194e47a29aa46908b0d10/jsonschema_path/paths.py#L86

Why is this needed?

Ideally I would write the above as:

spec_path = "path/to/spec.yaml"
openapi = openapi_core.OpenAPI.from_file_path(spec_path)

and the spec_base_uri would be correctly set.

The base_uri is technically available but is private, e.g.:

base_uri = openapi.spec.accessor.resolver._base_uri

References

No response

Would you like to implement a feature?

None