[java][jersey] Fix ALLOW_COERCION_OF_SCALARS (#14619)

* Remove dead code

* Refactor containsKey before get

* Minor refactor

* Use JsonMapper.builder() to build ObjectMapper

Fix a bug where ALLOW_COERCION_OF_SCALARS wasn't set properly.

* Update samples

* Oops, fix a typo
This commit is contained in:
Robin Karlsson
2023-02-06 03:34:21 +01:00
committed by GitHub
parent 31c3a40b4c
commit ca19fa0ef3
18 changed files with 235 additions and 289 deletions

View File

@@ -384,9 +384,10 @@ public class ApiClient extends JavaTimeFormatter {
if (auth instanceof ApiKeyAuth) {
String name = authEntry.getKey();
// respect x-auth-id-alias property
name = authenticationLookup.containsKey(name) ? authenticationLookup.get(name) : name;
if (secrets.containsKey(name)) {
((ApiKeyAuth) auth).setApiKey(secrets.get(name));
name = authenticationLookup.getOrDefault(name, name);
String secret = secrets.get(name);
if (secret != null) {
((ApiKeyAuth) auth).setApiKey(secret);
}
}
}
@@ -902,11 +903,6 @@ public class ApiClient extends JavaTimeFormatter {
return file;
}
String contentType = null;
List<Object> contentTypes = response.getHeaders().get("Content-Type");
if (contentTypes != null && !contentTypes.isEmpty())
contentType = String.valueOf(contentTypes.get(0));
// read the entity stream multiple times
response.bufferEntity();
@@ -1008,14 +1004,11 @@ public class ApiClient extends JavaTimeFormatter {
boolean isBodyNullable)
throws ApiException {
// Not using `.target(targetURL).path(path)` below,
// to support (constant) query string in `path`, e.g. "/posts?draft=1"
String targetURL;
if (serverIndex != null && operationServers.containsKey(operation)) {
Integer index = operationServerIndex.containsKey(operation) ? operationServerIndex.get(operation) : serverIndex;
Map<String, String> variables = operationServerVariables.containsKey(operation) ?
operationServerVariables.get(operation) : serverVariables;
List<ServerConfiguration> serverConfigurations = operationServers.get(operation);
List<ServerConfiguration> serverConfigurations;
if (serverIndex != null && (serverConfigurations = operationServers.get(operation)) != null) {
int index = operationServerIndex.getOrDefault(operation, serverIndex).intValue();
Map<String, String> variables = operationServerVariables.getOrDefault(operation, serverVariables);
if (index < 0 || index >= serverConfigurations.size()) {
throw new ArrayIndexOutOfBoundsException(
String.format(
@@ -1026,6 +1019,8 @@ public class ApiClient extends JavaTimeFormatter {
} else {
targetURL = this.basePath + path;
}
// Not using `.target(targetURL).path(path)` below,
// to support (constant) query string in `path`, e.g. "/posts?draft=1"
WebTarget target = httpClient.target(targetURL);
if (queryParams != null) {
@@ -1036,11 +1031,10 @@ public class ApiClient extends JavaTimeFormatter {
}
}
Invocation.Builder invocationBuilder;
Invocation.Builder invocationBuilder = target.request();
if (accept != null) {
invocationBuilder = target.request().accept(accept);
} else {
invocationBuilder = target.request();
invocationBuilder = invocationBuilder.accept(accept);
}
for (Entry<String, String> entry : cookieParams.entrySet()) {

View File

@@ -19,18 +19,18 @@ public class JSON implements ContextResolver<ObjectMapper> {
private ObjectMapper mapper;
public JSON() {
mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
JsonMapper.builder().configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, true);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
mapper.setDateFormat(new RFC3339DateFormat());
mapper.registerModule(new JavaTimeModule());
JsonNullableModule jnm = new JsonNullableModule();
mapper.registerModule(jnm);
mapper = JsonMapper.builder()
.serializationInclusion(JsonInclude.Include.NON_NULL)
.configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, true)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.defaultDateFormat(new RFC3339DateFormat())
.addModule(new JavaTimeModule())
.addModule(new JsonNullableModule())
.build();
}
/**