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

openapi: workaround issue importing fully resolved definitions #5299

Merged
merged 13 commits into from
Jul 2, 2024

Conversation

megalucio
Copy link
Contributor

@megalucio megalucio commented Feb 16, 2024

Overview

Fix import openapi definitions from files. It fails with big files.

Related Issues

Specify any related issues or pull requests by linking to them.

zaproxy/zaproxy#8193

Checklist

  • Update help
  • Update changelog
  • Run ./gradlew spotlessApply for code formatting
  • Write tests
  • Check code coverage
  • Sign-off commits
  • Squash commits
  • Use a descriptive title

For more details, please refer to the developer rules and guidelines.

Copy link

github-actions bot commented Feb 16, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@thc202 thc202 changed the title Fix import openapi from big files openapi: fix import of big files Feb 16, 2024
@megalucio
Copy link
Contributor Author

recheck

@thc202
Copy link
Member

thc202 commented Feb 16, 2024

You first need to add the agreeing comment.

@megalucio
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@megalucio

This comment has been minimized.

zapbot added a commit to zaproxy/cla that referenced this pull request Feb 16, 2024
@thc202
Copy link
Member

thc202 commented Feb 19, 2024

IMO this is not the proper fix, we should change the parser limits instead.

@megalucio
Copy link
Contributor Author

megalucio commented Feb 19, 2024

IMO this is not the proper fix, we should change the parser limits instead.

Agreed, however I understand the limit that is being reached here is the String's Maximum length(Integer.MAX_INT or 2147483647) as seen on the exception

com.fasterxml.jackson.databind.JsonMappingException: TextBuffer overrun: size reached (2147483648)
...
Caused by: java.lang.IllegalStateException: TextBuffer overrun: size reached (2147483648)
...

Not sure how to address that. Besides the method where the exception is being thrown, Json.pretty() belongs to external library swagger-core so it does not look like we can change that.

Another alternative which I was thinking was to catch for the IllegalStateException and only on that instance to get the string directly from the file, which seems a bit more proper.

Thoughts? Any other ideas on how to fix it?

@thc202
Copy link
Member

thc202 commented Feb 19, 2024

It looks suspicious that's actually hitting the max of the string, what's the size of the original definition? Did you try write the expanded definition to a file to see its size (and contents for that matter)?

I'm aware that's a class/method from the library, but that's "one" line of code.

Agreed that looks better still I'd skip the pretty print instead of reading from the file (as that would break the multi file).

@kingthorin
Copy link
Member

@megalucio are you able to answer the latest questions and keep going with this?

@megalucio
Copy link
Contributor Author

Giving another go to this one, @thc202 what do you mean by the original and expanded definitions? Not sure, I'm following here. Thanks

@thc202
Copy link
Member

thc202 commented Jul 1, 2024

In SwaggerConverter calls to resolveFully will replace the references with actual values which will make the definition bigger, i was suggesting saving that one and verify the contents and the size.

Would still be interesting to know the size of the original.

@megalucio
Copy link
Contributor Author

Size of the original is 2.3M

@thc202
Copy link
Member

thc202 commented Jul 1, 2024

And just before the Json.pretty(…)?

@megalucio
Copy link
Contributor Author

openApiString.length() = 2378052

@megalucio
Copy link
Contributor Author

Not sure how to calculate the size there since I have an OpenAPI object

@thc202
Copy link
Member

thc202 commented Jul 1, 2024

With Json.mapper().writeValueAsString(…) (or Yaml depending what format you have), you can also write to file with one of the other methods of that class.

@megalucio
Copy link
Contributor Author

It looks suspicious that's actually hitting the max of the string, what's the size of the original definition? Did you try write the expanded definition to a file to see its size (and contents for that matter)?

I'm aware that's a class/method from the library, but that's "one" line of code.

Agreed that looks better still I'd skip the pretty print instead of reading from the file (as that would break the multi file).

Honestly I'm not sure what you mean by the original definition, this is what is being provided. Still unable to process big openapi definitions.

With Json.mapper().writeValueAsString(…) (or Yaml depending what format you have), you can also write to file with one of the other methods of that class.

Yeah, that is where the exception is being thrown, this is what happens inside pretty. But the good thing about using it is that in this instance I can catch the exception and only read directly from file when that happens which respects more the previous implementation, I figure this is more backwards compatible. Only doubt I have is if still use Json.pretty or stick with Json.mapper().writeValueAsString(…)`. I will post a new commit now with this idea see what you think.

@thc202
Copy link
Member

thc202 commented Jul 1, 2024

Did you try write to file?

@megalucio
Copy link
Contributor Author

megalucio commented Jul 1, 2024

Did you try write to file?

If I am understanding you correctly, I get the same exception when Json.mapper().writeValueAsString so basically cant write to file.

@thc202
Copy link
Member

thc202 commented Jul 1, 2024

I mean one of the other methods not that one, e.g. writeValue(File, Object).

@megalucio
Copy link
Contributor Author

Yeah I got a 2.9 GB file 🤦‍♂️

@thc202 thc202 changed the title openapi: fix import of big files openapi: workaround issue importing fully resolved definitions Jul 2, 2024
@thc202
Copy link
Member

thc202 commented Jul 2, 2024

  • This needs a spotlessApply.
  • Can you add an entry to the changelog? Something like:
### Changed
- Workaround issue loading fully resolved definitions that are too large by trying to use the original definition only (Issue 8193).

@thc202
Copy link
Member

thc202 commented Jul 2, 2024

This might also need a merge/rebase.

@thc202
Copy link
Member

thc202 commented Jul 2, 2024

Did you fetch from upstream/zaproxy? (the PR has conflict now)

megalucio and others added 10 commits July 2, 2024 09:38
Signed-off-by: Ignacio Íñigo <megalucio@users.noreply.github.com>
Signed-off-by: Ignacio Íñigo <megalucio@users.noreply.github.com>
Signed-off-by: Ignacio Íñigo <megalucio@users.noreply.github.com>
Signed-off-by: Ignacio Íñigo <megalucio@users.noreply.github.com>
…/ExtensionOpenApi.java

Co-authored-by: thc202 <thc202@gmail.com>
Signed-off-by: Ignacio Íñigo Hernández <megalucio@users.noreply.github.com>
@megalucio
Copy link
Contributor Author

I think is ready now, please review again.

@thc202
Copy link
Member

thc202 commented Jul 2, 2024

I think it still needs a spotlessApply.

@thc202 thc202 enabled auto-merge (squash) July 2, 2024 10:09
@thc202
Copy link
Member

thc202 commented Jul 2, 2024

Thank you!

@thc202 thc202 merged commit 9ae00ac into zaproxy:main Jul 2, 2024
10 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jul 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants