ioggstream / openapi-resolver Goto Github PK
View Code? Open in Web Editor NEWA python 3 module for resolving $refs in OpenAPI specs and create bundles.
License: MIT License
A python 3 module for resolving $refs in OpenAPI specs and create bundles.
License: MIT License
References like:
components:
headers:
X-RateLimit-Limit:
$ref: 'https://raw.githubusercontent.com/teamdigitale/openapi/0.0.3/docs/definitions.yaml#/headers/X-RateLimit-Limit'
to be resolved correctly
They are resolved like
components:
headers:
headers/X-RateLimit-Limit:
...
When my top-level OpenAPI file has a ref:
$ref: user/User.yaml
and that file also has a ref:
$ref: CreateUser.yaml
openapi-resolver looks for CreateUser.yaml in the top-level directory instead of the user subdirectory.
$ python -m openapi_resolver api.yaml
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__main__.py", line 32, in <module>
main(args.src_file, args.dst_file)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__main__.py", line 17, in main
resolver.resolve()
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 98, in resolve
self.traverse(self.openapi, cb=self.resolve_node)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 194, in traverse
self.traverse(i, k, parents, cb, context)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 194, in traverse
self.traverse(i, k, parents, cb, context)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 194, in traverse
self.traverse(i, k, parents, cb, context)
[Previous line repeated 6 more times]
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 227, in traverse
self.traverse(ancestor[needle], key, parents, cb, context)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 194, in traverse
self.traverse(i, k, parents, cb, context)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 194, in traverse
self.traverse(i, k, parents, cb, context)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 194, in traverse
self.traverse(i, k, parents, cb, context)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 210, in traverse
ancestor[needle] = cb(key, node, context)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 263, in resolve_node
_yaml = self.get_yaml_reference(n)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 243, in get_yaml_reference
self.yaml_cache[host] = open_file_or_url(host)
File "/home/boris/iron_ox/server/.venv/lib/python3.6/site-packages/openapi_resolver/__init__.py", line 63, in open_file_or_url
with open(host) as fh:
FileNotFoundError: [Errno 2] No such file or directory: 'CreateUser.yaml'
everything to be reformatted with black
and isort
automagically.
To support remote files
python -mopenapi_resolver https://foo.com/openapi.yaml
The context (namely the URL associated to the $ref
to process) to be fully managed in the stack.
There's an instance variable too.
relative references in external sources are correctly resolved.
they are not
openapi resover to collect all referenced items under components
Tests could just verify that main()
works as expected.
Buongiorno Roberto
Una richiesta di comportamento su openapi-resolver
.
Ho un file denominato openapi.yaml
che contiene:
schemas:
myLongitude:
$ref: 'https://teamdigitale.github.io/openapi/master/definitions.yaml#/schemas/Longitude'
eseguo openapi-resolver
e ottengo:
schemas:
Longitude:
description: |-
Longitude of a point expressed in:
* the ETRS89 system for Italian and European territories
* and in WGS84 for the others.
example: 9.19289
externalDocs:
url: http://dati.gov.it/onto/clvapit#long
format: double
maximum: 180
minimum: -180
type: number
myLongitude:
$ref: '#/components/schemas/Longitude'
Tutto ok.
Il file openapi.yaml
contiene un riferimento ad un mio file definitions.yaml
:
schemas:
myLongitude:
$ref: 'https://ingv.github.io/openapi/definitions.yaml#/schemas/longitude'
il file definitions.yaml
contiene un riferimento esterno:
schemas:
longitude:
$ref: 'https://teamdigitale.github.io/openapi/master/definitions.yaml#/schemas/Longitude'
eseguo openapi-resolver
e ottengo:
schemas:
Longitude:
description: |-
Longitude of a point expressed in:
* the ETRS89 system for Italian and European territories
* and in WGS84 for the others.
example: 9.19289
externalDocs:
url: http://dati.gov.it/onto/clvapit#long
format: double
maximum: 180
minimum: -180
type: number
myLongitude:
$ref: '#/components/schemas/longitude'
longitude:
$ref: 'https://teamdigitale.github.io/openapi/master/definitions.yaml#/schemas/Longitude'
il problema e' che ho un Warning
da Swagger su Longitude
in quanto nel codice non viene usata. Per il resto, tutto funziona perfettamente; Swagger espande il riferimento esterno per longitudine
e tutto torna.
a more descriptive error when local components are missing
a simple solution could be to validate yaml before parsing.
This should be done:
Important:
yaml.safe_load everywhere
Indirect references to be resolved, eg:
openapi.yaml#/components/schemas/Pointer2
-> indirect.yaml#/schemas/Pointer1
-> store.yaml#/schemas/Pointer
Eg:
--- openapi.yaml
components:
schemas:
Pointer2:
$ref: indirect.yaml#/schemas/Pointer1
--- pointer.yaml
schemas:
Pointer1:
$ref: original.yaml#/schemas/Pointer
--- original.yaml
schemas:
Pointer:
type: string
format: pointer
should contain
--- resolved.yaml
components:
schemas:
Pointer:
type: string
format: pointer
Hi @ioggstream
I'm using your package with our openapi.yaml
spec (attached):
docker run -it --rm -v $(pwd)/0.0.3:/code openapi-resolver /code/openapi.yaml /code/openapi-res.yaml
and I noticed that It generates a wrong structure.
The schema:
parameters:
. . .
provenance__parameters:
default: null
description: Name/URI/DOI of the parameters | varchar(255)
example: ToDo
type: string
is pushed into parameters:
object but should be in schemas:
.
In addition, in the object ObjectProvenance
:
ObjectProvenance:
properties:
description:
$ref: '#/components/schemas/provenance__description'
hostname:
$ref: '#/components/schemas/provenance__hostname'
method:
$ref: '#/components/schemas/provenance__method'
model:
$ref: '#/components/schemas/provenance__model'
name:
$ref: '#/components/schemas/provenance__name'
parameters:
$ref: '#/components/parameters/provenance__parameters'
the property
called parameters
contains a wrong ref.; It should be:
$ref: '#/components/schemas/provenance__parameters'
instead of:
$ref: '#/components/parameters/provenance__parameters'
Thank you,
Valentino
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.