swagger-api / swagger-api/swagger-codegen

[JavaSpring]swagger-codegen-maven-plugin with <async>false</async>

Open
#8,759 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Hi,
I am using the swagger-codegen-maven-plugin with these versions: 2.3.1 and 2.4.0-SNAPSHOT (thats master i think).
I came across this bug. Please see code snippet below of the pom.xml alongwith my suggestions for the fix.. I think its not resolved yet.
If you feel its already fixed please excuse.
Thanks.
R
Please note value of asynch: false.

`

<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${swagger.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>
`

For the above configuration in swagger-codegen\src\main\resources\JavaSpring\apiDelegate.mustache an expression of {{#async}}CompletableFuture.completedFuture({{/async}} should resolve to empty. Unfortunately : In swagger-codegen\src\main\java\io\swagger\codegen\DefaultGenerator.java the value of async in this.config.additionalProperties() is a string.

This causes wrong code generation.

I can think of three solutions:

  1. In swagger-codegen\src\main\java\io\swagger\codegen\languages\SpringCodegen.java in public void processOpts() method change these lines:
    if (additionalProperties.containsKey(ASYNC)) {
    this.setAsync(Boolean.valueOf(additionalProperties.get(ASYNC).toString()));
    }
    TO
    if (additionalProperties.containsKey(ASYNC)) {
    this.setAsync(Boolean.valueOf(additionalProperties.get(ASYNC).toString()));
    convertPropertyToBooleanAndWriteBack(ASYNC);
    }

  2. Another way is in swagger-codegen\src\main\java\io\swagger\codegen\DefaultGenerator.java in method public Generator opts(ClientOptInput opts) right after this line
    "this.config = opts.getConfig();"
    invoke
    'adjustToBoolean("async");'
    this second approach has benefit that you can use it from one place than do this change from many places.
    I also think there may be other similar boolean properties that need similar handling.
    //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);

} 

`
Adding a a small google doc with more details using the 2nd approach
https://docs.google.com/document/d/1N7ASyYPAuk5gO_kqcggXHOG_w5nGlKlToy2H1bbG6Tg/edit?usp=sharing

  1. A third approach could be to eliminate the booleans like false being created as strings in the first place. That is something havent pursued.

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.

Research direction

Start with swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java and languages/SpringCodegen.java, then inspect swagger-codegen/src/main/resources/JavaSpring/apiDelegate.mustache. Trace how the Maven plugin's async value reaches additionalProperties. Done means generating the shown Spring configuration with async=false no longer emits CompletableFuture.completedFuture code.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.