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

[dev-2.0] RefResolver cannot resolve yaml #681

Closed
dtkav opened this issue Sep 14, 2018 · 4 comments
Closed

[dev-2.0] RefResolver cannot resolve yaml #681

dtkav opened this issue Sep 14, 2018 · 4 comments
Assignees

Comments

@dtkav
Copy link
Collaborator

dtkav commented Sep 14, 2018

connexion/jsonref.py:from jsonschema import RefResolver

because it can't parse yaml schema files like https://opensource.zalando.com/problem/schema.yaml

I workaround'd it in the jsonschema module


+++ b/jsonschema/validators.py
@@ -2,6 +2,7 @@ from __future__ import division
 
 import contextlib
 import json
+import yaml
 import numbers
 
 try:
@@ -466,12 +467,15 @@ class RefResolver(object):
             # Requests has support for detecting the correct encoding of
             # json over http
             if callable(requests.Response.json):
-                result = requests.get(uri).json()
-            else:
-                result = requests.get(uri).json
+                try:
+                    result = requests.get(uri).json()
+                except ValueError:
+                    result = None
+            if not result:
+               result = yaml.load(requests.get(uri).content)
         else:
             # Otherwise, pass off to urllib and assume utf-8
-            result = json.loads(urlopen(uri).read().decode("utf-8"))
+           result = yaml.load(urlopen(uri).read().decode("utf-8"))
 
         if self.cache_remote:

But on https://github.com/Julian/jsonschema/issues/420 it seems yaml is currently out of scope.

Can we fix it in connexion subclassing RefResolver?

Originally posted by @ioggstream in #678 (comment)

@dtkav
Copy link
Collaborator Author

dtkav commented Sep 14, 2018

Hey @ioggstream let me know if python-jsonschema/jsonschema#682 fixes your issue!

@ioggstream
Copy link
Contributor

Thanks @dtkav.

This generally work! You did a wonderful job!

I create a simple nosetests that could be helpful in this validating external yamls
(this adds a FakeResolver dynamically adding functions to the module)

https://gist.github.com/ioggstream/e50ad5c1405f44e7ec266e1016fa2a29

As of now:

  • zapp.options.oas_version == (2,0) even for oas2 files.

Q: can I get the spec back from the parsing? It could be interesting to test the resulting yaml back.

@dtkav
Copy link
Collaborator Author

dtkav commented Sep 17, 2018

cool @ioggstream - If you want to, you could modify that into a unit test and submit it as a PR to run in the test suite.

Q: can I get the spec back from the parsing? It could be interesting to test the resulting yaml back.

app = connexion.FlaskApp(__name__)
api = app.add_api('openapi.yaml')
api.raw_spec       # raw spec
api.specification  # parsed spec

@Guru67724
Copy link

Guru67724 commented Nov 27, 2018

connexion/jsonref.py:from jsonschema import RefResolver

because it can't parse yaml schema files like https://opensource.zalando.com/problem/schema.yaml

I workaround'd it in the jsonschema module


+++ b/jsonschema/validators.py
@@ -2,6 +2,7 @@ from __future__ import division
 
 import contextlib
 import json
+import yaml
 import numbers
 
 try:
@@ -466,12 +467,15 @@ class RefResolver(object):
             # Requests has support for detecting the correct encoding of
             # json over http
             if callable(requests.Response.json):
-                result = requests.get(uri).json()
-            else:
-                result = requests.get(uri).json
+                try:
+                    result = requests.get(uri).json()
+                except ValueError:
+                    result = None
+            if not result:
+               result = yaml.load(requests.get(uri).content)
         else:
             # Otherwise, pass off to urllib and assume utf-8
-            result = json.loads(urlopen(uri).read().decode("utf-8"))
+           result = yaml.load(urlopen(uri).read().decode("utf-8"))
 
         if self.cache_remote:

But on Julian/jsonschema#420 it seems yaml is currently out of scope.

Can we fix it in connexion subclassing RefResolver?

Originally posted by @ioggstream in #678 (comment)

Hi @dtkav ,

How can i use this now to validate my yaml files.?
Because i got stuck up in this error.

json.loads(urlopen(uri).read().decode("utf-8"))

when it tries to work with yaml files. Any config option available to make it to work with yaml.
When i tried
yaml.load(urlopen(uri).read().decode("utf-8"))

it worked fine for me.

Not sure whether this has been integrated into the latest available release..

Thanks
GuruPrasad

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

No branches or pull requests

4 participants