[JavaSpring]openapi-generator-maven-plugin with <async>false</async>

Open
#1,164 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java

Research direction

Start by reproducing the Maven configuration with false and inspect DefaultGenerator.java alongside JavaSpring/methodBody.mustache. Then inspect JavaSpring/libraries/spring-boot/openapi2SpringBoot.mustache for the separate compilation issue. Done means generated Spring code handles the async setting correctly and the reported Maven compilation failure is resolved, with the two concerns scoped clearly.

Written by the indexing model from the issue text.

Description

Hi,
I am using the openapi-generator-maven-plugin with these versions: 3.3.1-SNAPSHOT (thats master ).
I came across this bug. Please see code snippet below of the pom.xml alongwith my suggestions for the fix.. Let me know if I should raise a PR.

I have also noted another smaller issue unrelated to this.
Thanks.
R
Please note value of asynch: false.

`

            <plugin>
                          <groupId>org.openapitools</groupId>
			<artifactId>openapi-generator-maven-plugin</artifactId>
			<version>${openapi.codegen.version}</version>
			<executions>
				<execution>
					<id>common</id>
					<goals>
						<goal>generate</goal>
					</goals>
					<configuration>
                                              <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
						<modelPackage>com.example.api.models</modelPackage>
						<apiPackage>com.example.api</apiPackage>
						<output>${project.basedir}</output>					
						<language>spring</language>
						<invokerPackage>com.example.api</invokerPackage>
						<basePackage>com.example.api</basePackage>
						<withXml>true</withXml>
						<configOptions>
						
							<artifactId>foo</artifactId>
							<artifactDescription>Test API</artifactDescription>
							<title>Test API</title>
							<artifactUrl>https://api.example.com</artifactUrl>
							<groupId>com.example.api</groupId>
							<artifactVersion>1</artifactVersion>								
							<sourceFolder>src/gen/java/main</sourceFolder>
							<serializableModel>true</serializableModel>
							<dateLibrary>java8</dateLibrary>
							<java8>true</java8>
							<async>false</async>
							<library>spring-boot</library>		
							<delegatePattern>true</delegatePattern>
							<useBeanValidation>true</useBeanValidation>
							<useOptional>true</useOptional>	
							<hideGenerationTimestamp>true</hideGenerationTimestamp>								
						</configOptions>							
					</configuration>
				</execution>
			</executions>
		</plugin>

`

For the above configuration in openapi-generator\modules\openapi-generator\src\main\resources\JavaSpring\methodBody.mustache an expression of
{{#async}}CompletableFuture.completedFuture({{/async}} should resolve to empty. Unfortunately : In openapi-generator\modules\openapi-generator\src\main\java\org\openapitools\codegen\DefaultGenerator.java the value of async in this.config.additionalProperties() is a string and not a boolean.

This causes wrong code generation.

I can think of a few solutions of which to my mind the most effective one is this:
In openapi-generator\modules\openapi-generator\src\main\java\org\openapitools\codegen\DefaultGenerator.java in method public Generator opts(ClientOptInput opts) right after this line
"this.config = opts.getConfig();"
invoke
'adjustToBoolean("async");'

//new method

`
public void adjustToBoolean(String propertyName)
{
Object object = this.config.additionalProperties().get(propertyName);
Boolean val=Boolean.FALSE;
if(object!=null)
{
if(object instanceof String)
{
String string=(String) object;
if(string.equalsIgnoreCase("true"))
{
val=true;
}
else
{
val=Boolean.FALSE;
}
}
else if(object instanceof Boolean)
{
val=(Boolean) object;
}
else
{
val=Boolean.TRUE;
}
}
else
{
val=Boolean.FALSE;
}
this.config.additionalProperties().put(propertyName, val);

} 

`
Note: In addition to the above problem and unrelated to this there is a generated code in the generated OpenAPI2SpringBoot.java

@Bean
public WebMvcConfigurer webConfigurer() {
return new WebMvcConfigurer() {
/* @Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("")
.allowedMethods("
")
.allowedHeaders("Content-Type");
} */
};
}

This code is not fully implemented and seems mostly commented.
Maven based compilation causes compilation error here.
This can be fixed by changing in openapi-generator\modules\openapi-generator\src\main\resources\JavaSpring\libraries\spring-boot\openapi2SpringBoot.mustache
the following lines:

@Bean
public Web{{^reactive}}Mvc{{/reactive}}{{#reactive}}Flux{{/reactive}}Configurer webConfigurer() {
return new Web{{^reactive}}Mvc{{/reactive}}
{{#reactive}}Flux{{/reactive}}Configurer{{^java8}}Adapter{{/java8}}() {
/* @Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("")
.allowedMethods("
")
.allowedHeaders("Content-Type");
} */
{{^useSpringfox}}

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/3.14.2/");
        }
      {{/useSpringfox}}
    };
}

TO

/* @Bean
public Web{{^reactive}}Mvc{{/reactive}}{{#reactive}}Flux{{/reactive}}Configurer webConfigurer() {
return new Web{{^reactive}}Mvc{{/reactive}}{{#reactive}}Flux{{/reactive}}Configurer{{^java8}}Adapter{{/java8}}() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("")
.allowedMethods("
")
.allowedHeaders("Content-Type");
}
{{^useSpringfox}}

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/3.14.2/");
        }

{{/useSpringfox}}
};
}
*/

I can include this change also in the PR
Note: I have not yet learnt how to show the code blocks properly in this issue editor. I hope everything is clear in spite of that.

Dominant language
Java
Stars
26.8k
Forks
7.7k
Avg merge
1d 6h
Merged PRs (30d)
100

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from OpenAPITools/openapi-generator

All issues in OpenAPITools/openapi-generator

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.