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

[BUG][Java][Spring] error: constant string too long #2186

Closed
TruthNZ opened this issue Feb 18, 2019 · 8 comments · Fixed by #2903
Closed

[BUG][Java][Spring] error: constant string too long #2186

TruthNZ opened this issue Feb 18, 2019 · 8 comments · Fixed by #2903

Comments

@TruthNZ
Copy link

TruthNZ commented Feb 18, 2019

With a large enough data model the Java-Spring server generator produces example responses with a string that is too long for the Java compiler.
It's not exceeding the size of a string allowed by the JVM, but the size of a string constant the compiler handles.

 default ResponseEntity<ResponseObject> restMethod(@ApiParam(value = "A Parameter",required=true) @PathVariable("parameter") String parameter) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    ApiUtil.setExampleResponse(request, "application/json", "{ [A large example json string response that breaks the Java compiler] }");
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }

Was generated using openapi-generator-maven-plugin 3.3.4.
Settings:

<configOptions>
	<sourceFolder>${generated-sources-java-path}</sourceFolder>
	<interfaceOnly>true</interfaceOnly>
	<java8>true</java8>
	<dateLibrary>java8</dateLibrary>
	<useBeanValidation>true</useBeanValidation>
	<performBeanValidation>true</performBeanValidation>
</configOptions>
<generatorName>spring</generatorName>

Potentially similar to the swagger codegen issue: swagger-api/swagger-codegen#9055

Is an example response needed?
Failing that: Could it be broken into a StringBuilder (or at worst just string concatenation)? Or maybe loaded from a resource file?

@wing328
Copy link
Member

wing328 commented Feb 20, 2019

@TruthNZ does setting the "example" in the spec give you control on the example response?

@TruthNZ
Copy link
Author

TruthNZ commented Feb 20, 2019

We have examples set in the spec. We've set them at the individual field level (large document structure with various reused elements) - and we definitely want them for our api consumers to have a good idea of formatting requirements, etc.
In the codegen it looks like it's concatenating them all (+ standard examples for fields we haven't explicitly specified) into an overall example response document - hence the massive example string.

But even if we modify it on our specific project, I believe the overall bug still needs to be dealt with.

@wing328
Copy link
Member

wing328 commented Feb 20, 2019

OK. Can you share the exact error message thrown by JVM?

@TruthNZ
Copy link
Author

TruthNZ commented Feb 20, 2019

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Development/AProject/src/main/java/com/company/project/api/RestInterfaceApi.java:[53,77] constant string too long
[INFO] 1 error

@wing328
Copy link
Member

wing328 commented Feb 20, 2019

What about looping through all examples via postProcessOperationsWithModels and set the example to null or empty string if the string exceeds a certain size (and output a warning as well) ?

@inthegarage
Copy link

inthegarage commented Oct 3, 2019

Is it possible to remove this line altogether or trim it, currently some of the sample values are using the full extent of the value, IE a number is 5.6373766566333287642009963747113943099975585937

Is there any option to remove this at all? I don't really need this example response in the interface.

@wing328
Copy link
Member

wing328 commented Oct 3, 2019

@inthegarage maybe removing it from the spec as a workaround for the time being?

@inthegarage
Copy link

@wing328 How do you mean "spec"? Is this the yaml file used to created the API or something else?

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

Successfully merging a pull request may close this issue.

3 participants