Skip to content

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

@jaaufauvre

Description

@jaaufauvre

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions