oapi.oas.references
This module provides functionality for resolving references within an instance
of oapi.oas.OpenAPI
.
The following will replace all references in the Open API
document open_api_document
with the objects targeted by the ref
property
of the reference.
Example:
import yaml
from urllib.request import urlopen
from oapi.oas import OpenAPI
from oapi.oas.references import Resolver
with urlopen(
"https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.1.1/"
"examples/v3.0/callback-example.yaml"
) as response:
open_api_document = OpenAPI(
yaml.safe_load(response)
)
resolver = Resolver(open_api_document)
resolver.dereference()
Resolver
Resolver(
root: oapi.oas.model.OpenAPI,
url: str | None = None,
urlopen: typing.Callable = oapi.oas.references._urlopen,
)
This class should be used, with an instance of oapi.oas.OpenAPI
, to
resolve references.
Parameters:
-
root
(oapi.oas.model.OpenAPI
) –The OpenAPI document against which pointers will be resolved.
-
url
(str | None
, default:None
) –The URL or file path from where
root
was retrieved. The base URL for relative paths will be the directory above this URL. This will not typically be needed, as it can be inferred from mostModel
instances. -
urlopen
(typing.Callable
, default:oapi.oas.references._urlopen
) –If provided, this should be a function taking one argument (a
str
), which can be used in lieu ofurllib.request.urlopen
to retrieve a document and return an instance of a sub-class ofIOBase
(such ashttp.client.HTTPResponse
). This should be used if authentication is needed in order to retrieve external references in the document, or if local file paths will be referenced instead of web URL's (useopen
as the value for theurlopen
parameter in this case).
Source code in src/oapi/oas/references.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
|
get_document
get_document(url: str) -> oapi.oas.references._Document
Retrieve a document by URL, or use the cached document if previously retrieved
Source code in src/oapi/oas/references.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|
dereference
dereference() -> None
Dereference the primary document
Source code in src/oapi/oas/references.py
413 414 415 416 417 |
|
resolve
resolve(
pointer: str,
types: (
sob.abc.Types
| collections.abc.Sequence[type | sob.abc.Property]
) = (),
*,
dereference: bool = False
) -> sob.abc.Model
Retrieve an object at the specified pointer
Source code in src/oapi/oas/references.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
|
resolve_reference
resolve_reference(
reference: oapi.oas.model.Reference,
types: (
sob.abc.Types
| collections.abc.Sequence[type | sob.abc.Property]
) = (),
) -> sob.abc.Model
Retrieve a referenced object.
Parameters:
-
reference
(oapi.oas.model.Reference
) – -
types
(sob.abc.Types | collections.abc.Sequence[type | sob.abc.Property]
, default:()
) –
Source code in src/oapi/oas/references.py
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
|
get_relative_url
get_relative_url(url: str) -> str
Given a URL, return that URL relative to the base document
Source code in src/oapi/oas/references.py
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
|