Skip to content

Can't generate new project with OpenAPI JSON file #488

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

Closed
Gelbpunkt opened this issue Sep 5, 2021 · 6 comments · Fixed by #515
Closed

Can't generate new project with OpenAPI JSON file #488

Gelbpunkt opened this issue Sep 5, 2021 · 6 comments · Fixed by #515
Labels
🐞bug Something isn't working
Milestone

Comments

@Gelbpunkt
Copy link

Describe the bug
The generator expects a YAML file, and doesn't work with JSON files, despite all examples using JSON files.

To Reproduce
Steps to reproduce the behavior:

  1. openapi-python-client generate --url https://raw.githubusercontent.com/Nadybot/Nadybot/unstable/html/api.json
  2. See error message about invalid YAML

Expected behavior
I expect it to load the file with JSON if YAML fails. openapi_python_client/__init__.py in _get_document only attempts to load with YAML, replacing yaml.safe_load(yaml_bytes) with json.loads(yaml_bytes) "fixes" this issue.

OpenAPI Spec File
https://raw.githubusercontent.com/Nadybot/Nadybot/unstable/html/api.json

Desktop (please complete the following information):

  • OS: Fedora 34
  • Python Version: 3.9.6
  • openapi-python-client version 0.10.3
@Gelbpunkt Gelbpunkt added the 🐞bug Something isn't working label Sep 5, 2021
@dbanty
Copy link
Collaborator

dbanty commented Sep 5, 2021

Thanks for reporting the issue @Gelbpunkt, the problem actually isn't that the generator doesn't support JSON, YAML is a superset of JSON so YAML parsers naturally also parse JSON. In fact, if I run your JSON through a reformatter it suddenly starts parsing just fine. There's clearly still a bug here in that the parser sure doesn't like that JSON Which appears to be perfectly valid.

I'm going to poke at this a while and see if I can come up with a minimal reproducible example to narrow down where the bug is. For now curl https://raw.githubusercontent.com/Nadybot/Nadybot/unstable/html/api.json | jq > spec.json followed by openapi-python-client generate --path spec.json runs the generator... until it hits the bug in #479 which I'll hopefully resolve this weekend 😬

@Gelbpunkt
Copy link
Author

I didn't run into #479 at all, the only issue I experienced was #487 after I replaced the yaml.safe_load with json.loads. Apart from that, the generator works as expected 😀

@dbanty
Copy link
Collaborator

dbanty commented Sep 5, 2021

Interestingly if I run the generator from source I also don't get #479, only when I run the installed-in-pipx version against the reformatted JSON.

@tardyp
Copy link
Contributor

tardyp commented Oct 11, 2021

YAML is a superset of JSON so YAML parsers naturally also parse JSON.

Well that is not really the case. JSON supports tab as whitespace, but yaml don't.
I reproduce the same issue with a openapi json containing tabs ('\t')

yaml will not parse it:

yaml.scanner.ScannerError: while scanning for the next token
found character '\t' that cannot start any token
  in "[redacted].json", line 2, column 1

@dbanty
Copy link
Collaborator

dbanty commented Oct 11, 2021

Interesting, perhaps this is a bug in the YAML parser then because YAML should support tabs in the JSON-like flow syntax.

https://yaml.org/spec/1.2.2/#55-white-space-characters

@dbanty
Copy link
Collaborator

dbanty commented Oct 17, 2021

Thanks for the report @Gelbpunkt and the fix @tardyp! Fix is on main and will be in the 0.10.6 release (hopefully this weekend).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment