OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] JSON Parse Error in Deserializing Model Response
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
I am using OpenAPI Generate to create an SDK using OpenAPI Spec. After generating my code I get methods like usersGet and models like GetUsersResponse. When I call this method in my application code I call this generated code method like below:
ApiClient apiClient = new ApiClient();
apiClient.setDebugging(true);
...
UsersApi usersApi = new UsersApi(apiClient);
System.out.println(usersApi.usersGet());
Since I am not passing in a restTemplate I am using the generated resttemplate in my ApiClient that looks like this (this is part of the generated code):
/**
* Build the RestTemplate used to make HTTP requests.
* @return RestTemplate
*/
protected RestTemplate buildRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
// This allows us to read the response more than once - Necessary for debugging.
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
// disable default URL encoding
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory();
uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
restTemplate.setUriTemplateHandler(uriBuilderFactory);
return restTemplate;
}
The Issue
When I call this method I do get a response in my logs but then it eventually crashes, this is the response I get back at first:
{"@timestamp":"2023-03-27T16:53:34.072-05:00","@version":"1","message":"Response Body: [{\"id\":\"2323-9515-4f51-e184-3034fc028297\",\"firstName\":\"Joe\",\"lastName\":\"Sam\",\"role\":\"ADMIN\",\"email\":\"Joe.Sam@company.com\",\"enabled\":true}, {\"id\":\"232-9515-4f51-e184-2323\",\"firstName\":\"Suzy\",\"lastName\":\"Joe\",\"role\":\"ADMIN\",\"email\":\"Suzy.Joe@company.com\",\"enabled\":true}]
Then I get this issue (Error while extracting response for type [class ...] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type com.custody.sdk.fireblocks.model.GetUsersResponse from Array value (token JsonToken.START_ARRAY):
Exception in thread "main" org.springframework.web.client.RestClientException: Error while extracting response for type [class com.custody.sdk.fireblocks.model.GetUsersResponse] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `com.custody.sdk.fireblocks.model.GetUsersResponse` from Array value (token `JsonToken.START_ARRAY`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `com.custody.sdk.fireblocks.model.GetUsersResponse` from Array value (token `JsonToken.START_ARRAY`)
openapi-generator version
I am using 6.2.1, the gradle plugin
id("org.openapi.generator") version "6.2.1"
OpenAPI declaration file content or url
Generation Details
Below is the configuration for the code generation:
openApiGenerate {
generatorName = "java"
inputSpec = spec
outputDir = generatedCode
apiPackage = "${package_name}.controller".toString()
invokerPackage = "${package_name}.invoker".toString()
modelPackage = "${package_name}.model".toString()
generateAliasAsModel = false
generateModelTests = false
generateModelDocumentation = false
generateApiTests = false
generateApiDocumentation = false
skipValidateSpec = true
configOptions = [
sourceFolder: "src",
dateLibrary: "java11",
library: "resttemplate",
hideGenerationTimestamp: "true"
]
}```
##### Steps to reproduce
./gradlew build
or
./gradlew openApiGenerate
and then
./gradlew run
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the OpenAPI declaration URL and the Gradle openApiGenerate configuration, then compare the usersGet response schema with the generated UsersApi, GetUsersResponse, and ApiClient classes. Reproduce with ./gradlew openApiGenerate and ./gradlew run, and trace the response type used by the RestTemplate client. Done means the generated client handles the documented response shape without the shown Jackson deserialization error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, spring
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100