Skip to content

[RETROFIT2] Field string/binary not work correctly with retrofit2 library. #9653

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

Open
Arodrigal opened this issue Aug 20, 2019 · 1 comment

Comments

@Arodrigal
Copy link

Arodrigal commented Aug 20, 2019

Description

I am working in a Java environment. Java projects build the model and the libraries by connection/client type (feign, retrofit2, etc) with Swagger Codegen.
In any case, I have an API Rest service with a file field defined in the following way:
"type": "string",
"format": "byte"
The generated libraries have the corresponding model class with file field type byte[] but the same field in the Rest service class is generated with string type. This generates a casting error because the generated client doesn’t know how to convert a Base64 string to a byte[].

The retrofit2 library generates a JSON.java file with date adapters but ByteArrayAdapter is not added.

Swagger-codegen version

2.4.7

Swagger declaration file content or url
avatar:
	type: string
	format: binary
Command line used for generation
<profile>
<id>AndroidClientGeneration</id>
<dependencies>
	<dependency>
		<groupId>com.squareup.retrofit2</groupId>
	<artifactId>retrofit</artifactId>
	<version>2.5.0</version>
	</dependency>
	<dependency>
		<groupId>com.squareup.retrofit2</groupId>
		<artifactId>converter-gson</artifactId>
		<version>2.5.0</version>
	</dependency>
	<dependency>
		<groupId>com.squareup.retrofit2</groupId>
		<artifactId>converter-scalars</artifactId>
		<version>2.5.0</version>
	</dependency>
	<dependency>
		<groupId>com.google.code.gson</groupId>
		<artifactId>gson</artifactId>
		<version>2.8.5</version>
	</dependency>
	<dependency>
		<groupId>io.gsonfire</groupId>
		<artifactId>gson-fire</artifactId>
		<version>1.8.3</version>
	</dependency>
	<dependency>
	<groupId>org.threeten</groupId>
	<artifactId>threetenbp</artifactId>
	<version>1.4.0</version>
</dependency>
</dependencies>
<build>
	<plugins>
		<plugin>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-codegen-maven-plugin</artifactId>
			<version>${swagger.codegen.version}</version>
			<executions>
				<execution>
					<phase>generate-sources</phase>
					<goals>
						<goal>generate</goal>
					</goals>
					<configuration>
						<inputSpec>${api-definition}</inputSpec>
						<output>${generated-android-client-sources}</output>
						<ignoreFileOverride>${ignore-file-override}</ignoreFileOverride>
						<language>java</language>
						<library>retrofit2</library>
						<configOptions>
							<apiPackage>${package-client-codegen}.api</apiPackage>
							<modelPackage>${package-client-codegen}.model</modelPackage>
							<invokerPackage>${package-client-codegen}</invokerPackage>
							<licenseName>private</licenseName>
							<serializableModel>true</serializableModel>
							<useBeanValidation>true</useBeanValidation>
							<dateLibrary>threetenbp</dateLibrary>
						</configOptions>
					</configuration>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>build-helper-maven-plugin</artifactId>
			<executions>
				<execution>
					<id>add-generated-source</id>
					<phase>initialize</phase>
					<goals>
						<goal>add-source</goal>
					</goals>
					<configuration>
						<sources>
							<source>${generated-sources}</source>
						</sources>
					</configuration>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<skip>true</skip>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<configuration>
				<classifier>${android-client-classifier}</classifier>
				<classesDirectory>${project.build.outputDirectory}</classesDirectory>
				<includes>
					<include>${package-client-sources}</include>
				</includes>
			</configuration>
		</plugin>
	</plugins>
</build>
</profile>
Related issues/PRs

Similar issue for Java: #4824
Similar issue for C#: #7875

Suggest a fix/enhancement

// Register our new ByterArray type adapter
gsonBuilder.registerTypeAdapter(byte[].class, new ByteArrayAdapter(client));

@Arodrigal
Copy link
Author

I could help fixing and testing this issue.

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

No branches or pull requests

1 participant