Skip to content

handle unknown field in seeded tests #1254

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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.evomaster.client.java.controller.problem.rpc.schema.params;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.evomaster.client.java.controller.api.dto.problem.rpc.ParamDto;
import org.evomaster.client.java.controller.problem.rpc.schema.types.AccessibleSchema;
import org.evomaster.client.java.controller.problem.rpc.schema.types.PrimitiveOrWrapperType;
import org.evomaster.client.java.controller.problem.rpc.schema.types.TypeSchema;
import org.evomaster.client.java.utils.SimpleLogger;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,6 +23,9 @@ public abstract class NamedTypedValue<T extends TypeSchema, V> {

protected final static ObjectMapper objectMaper = new ObjectMapper();

protected final static ObjectMapper mapperAllowUnkownFields = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

/**
* name of the instance, eg param name
*/
Expand Down Expand Up @@ -225,7 +230,27 @@ else if (PrimitiveOrWrapperType.isPrimitiveOrTypes(json.getClass())){
}

public Object parseValueWithJson(String json) throws JsonProcessingException {
return objectMaper.readValue(json, getType().getClazz());
try{
return objectMaper.readValue(json, getType().getClazz());
}catch (JsonProcessingException e) {
/*
In the seeded tests of the industrial case study, there are cases where
unrecognized fields appear in the JSON input, such as:
{
"setType": true // Note: there is no corresponding 'setType' field in the target class
}

To handle such cases while still capturing the error message,
attempt to parse the JSON again with a configuration that allows unknown fields.
*/
if (e.getMessage().contains("Unrecognized field")) {
SimpleLogger.recordErrorMessage(
String.format("Try once to fix issues in json: %s", e.getMessage()));
return mapperAllowUnkownFields.readValue(json, getType().getClazz());
}
throw e;
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,30 @@ public List<SeededRPCTestDto> seedRPCTests() {
descriptiveInfo = "a scheduled task for invoking executeFlag";
}}
);
}},
new SeededRPCTestDto() {{
testName = "test_7";
rpcFunctions = Arrays.asList(
new SeededRPCActionDto() {{
interfaceName = FakeMockObjectService.Iface.class.getName();
functionName = "getFooFromExternalService";
inputParams = Arrays.asList("0");
inputParamTypes = Arrays.asList(int.class.getName());
mockRPCExternalServiceDtos = Arrays.asList(
new MockRPCExternalServiceDto() {{
appKey = "fake.app";
interfaceFullName = "com.foo.rpc.examples.spring.fakemockobject.external.fake.api.GetApiData";
functionName = "one";
responses = Arrays.asList("{\"exName\":\"abc\",\"exId\":0,\"exInfo\":[\"2025-05-28\"],\"unknownField\":\"unknown\"}");
responseTypes = Arrays.asList(
"com.foo.rpc.examples.spring.fakemockobject.external.fake.api.ExApiDto"
);
}}
);
}}
);
}}

);
}

Expand Down