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: fix import of big files #5299

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

megalucio
Copy link

@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
Author

recheck

@thc202
Copy link
Member

thc202 commented Feb 16, 2024

You first need to add the agreeing comment.

@megalucio
Copy link
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
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>
@@ -345,7 +345,7 @@ public OpenApiResults importOpenApiDefinitionV2(

List<String> errors =
importOpenApiDefinition(
Json.pretty(openApi),
new String(Files.readAllBytes(file.toPath())),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's this way to add support for multi file definitions, #2913, doing just this change breaks that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any good examples to test this multi file definition schema? I tried with https://github.com/Paldom/swagger-multifile-example/tree/master and in any case this does not seem to work either, getting different errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The codebase has tests for that, ExtensionOpenApiTest.shouldImportMultiFileV*.

@thc202
Copy link
Member

thc202 commented Feb 19, 2024

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

List<String> errors =
importOpenApiDefinition(
Json.pretty(openApi),
!openApiString.contains("openapi:") || openApiString.contains(".yaml#")
Copy link
Member

@thc202 thc202 Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks will not work for JSON format (in that it will still fail to parse big files).

@megalucio
Copy link
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants