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

Open
#9,653 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java
Domain
api, devtools

Research direction

Reproduce the Java retrofit2 generation using the reported string/binary schema and inspect the generated JSON.java file, focusing on the missing ByteArrayAdapter. Compare the output with the related Java issue #4824 and verify that generated byte[] fields can handle the service's Base64 string representation.

Written by the indexing model from the issue text.

Description

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));

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

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 swagger-api/swagger-codegen

All issues in swagger-api/swagger-codegen

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.