OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] IllegalArgumentException while parsing responses with unknown fields ❗

Open
#12,550 18 comments 19 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

API client libraries generated with OpenAPI Generator should ignore response fields that were not existing at the time the code was generated, instead of raising the following error:

java.lang.IllegalArgumentException: The field `updated` in the JSON string is not defined in the `Dog` properties. JSON: {"name":"Luna","created":"2021-04-23T17:32:28Z","updated":"2021-04-23T17:32:28Z"}
openapi-generator version
Version Status
OpenAPI Generator 5.4.0 ✔️
OpenAPI Generator 6.0.0
OpenAPI Generator 6.0.1-SNAPSHOT
OpenAPI declaration file content or url
  • sample-v1.yaml
  • sample-v2.yaml

Download ⤓

Generation Details
  • java -jar openapi-generator-cli-5.4.0.jar generate -g java -i sample-v1.yaml -c config.json -o api_client_5.4
  • java -jar openapi-generator-cli-6.0.0.jar generate -g java -i sample-v1.yaml -c config.json -o api_client_6.0
{ 
    "groupId" : "com.acme.app",
    "artifactId" : "sample-api-client",
    "artifactVersion" : "1.0.0",
    "invokerPackage" : "com.acme.app",
    "apiPackage" : "com.acme.app.api",
    "modelPackage" : "com.acme.app.model"
}
Steps to reproduce
  1. Generate a client from sample-v1.yaml:
java -jar openapi-generator-cli-6.0.0.jar generate -g java -i sample-v1.yaml -c config.json -o api_client_6.0
  1. Update DogsApiTest:
public class DogsApiTest {

    @Test
    public void addDogTest() throws ApiException {
        // GIVEN
        ApiClient client = new ApiClient();
        client.setDebugging(true);
        client.setBasePath("http://localhost:4010");
        NewDog newDog = new NewDog()
                .name("Bella");

        // WHEN
        Dog dog = new DogsApi(client).addDog(newDog);

        // THEN
        assertNotNull(dog);
    }

}
  1. Run prism.exe mock sample-v1.yaml
  2. Run addDogTest => ✔️
  3. Run prism.exe mock sample-v2.yaml
  4. Run addDogTest => ❌
java.lang.IllegalArgumentException: The field `updated` in the JSON string is not defined in the `Dog` properties. JSON: {"name":"Luna","created":"2021-04-23T17:32:28Z","updated":"2021-04-23T17:32:28Z"}

	at com.acme.app.model.Dog.validateJsonObject(Dog.java:217)
	at com.acme.app.model.Dog$CustomTypeAdapterFactory$1.read(Dog.java:253)
	at com.acme.app.model.Dog$CustomTypeAdapterFactory$1.read(Dog.java:243)
	at com.google.gson.TypeAdapter$1.read(TypeAdapter.java:199)
	at com.google.gson.Gson.fromJson(Gson.java:991)
	at com.google.gson.Gson.fromJson(Gson.java:956)
	at com.google.gson.Gson.fromJson(Gson.java:905)
	at com.acme.app.JSON.deserialize(JSON.java:151)
	at com.acme.app.ApiClient.deserialize(ApiClient.java:845)
	at com.acme.app.ApiClient.handleResponse(ApiClient.java:1053)
...

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 by generating the Java client from sample-v1.yaml and sample-v2.yaml, then inspect the generated Dog.java validateJsonObject path and the DogsApiTest reproduction. Trace the failure through JSON.deserialize and ApiClient.deserialize. Done means the generated client accepts the added updated response field without raising IllegalArgumentException.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.