-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix string-to-long coercion when afterburner is enabled
Reported upstream: FasterXML/jackson-modules-base#120 The CoercionConfig is fantastic, unfortunately it is not yet supported by the AfterBurner module. Separately we may consider moving to the new Blackbird module on java 11 runtimes. For now I've updated our existing long deserializers for 2.12.0 which has the benefit of rejecting coercion from string to SafeLong.
- Loading branch information
1 parent
946bd0a
commit 3239a08
Showing
5 changed files
with
202 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...erialization/src/main/java/com/palantir/conjure/java/serialization/LenientLongModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.conjure.java.serialization; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.cfg.CoercionAction; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.fasterxml.jackson.databind.exc.InvalidFormatException; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.fasterxml.jackson.datatype.jdk8.OptionalLongDeserializer; | ||
import com.palantir.logsafe.exceptions.SafeIoException; | ||
import java.io.IOException; | ||
import java.util.OptionalLong; | ||
|
||
/** | ||
* Provides support for the {@link Long} deserialization from JSON string and numeric values regardless of | ||
* | ||
* <pre>MapperFeature.ALLOW_COERCION_OF_SCALARS</pre> | ||
* | ||
* configuration. | ||
*/ | ||
final class LenientLongModule extends SimpleModule { | ||
|
||
LenientLongModule() { | ||
super("lenient long"); | ||
// Register to both Long.TYPE and Long.class | ||
this.addDeserializer(long.class, new LongAsStringDeserializer()) | ||
.addDeserializer(Long.class, new LongAsStringDeserializer()) | ||
.addDeserializer(OptionalLong.class, new OptionalLongAsStringDeserializer()); | ||
} | ||
|
||
private static final class LongAsStringDeserializer extends StdDeserializer<Long> { | ||
|
||
private LongAsStringDeserializer() { | ||
super(Long.TYPE); | ||
} | ||
|
||
@Override | ||
public Long deserialize(JsonParser jsonParser, DeserializationContext _ctxt) throws IOException { | ||
switch (jsonParser.currentToken()) { | ||
case VALUE_NUMBER_INT: | ||
return jsonParser.getLongValue(); | ||
case VALUE_STRING: | ||
return parseLong(jsonParser); | ||
case VALUE_NULL: | ||
return null; | ||
default: | ||
throw new SafeIoException("Expected a long value"); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isCachable() { | ||
return true; | ||
} | ||
|
||
private static Long parseLong(JsonParser jsonParser) throws IOException { | ||
String value = jsonParser.getValueAsString(); | ||
try { | ||
return Long.valueOf(value); | ||
} catch (NumberFormatException e) { | ||
InvalidFormatException failure = | ||
new InvalidFormatException(jsonParser, "not a valid long value", value, long.class); | ||
failure.initCause(e); | ||
throw failure; | ||
} | ||
} | ||
} | ||
|
||
private static final class OptionalLongAsStringDeserializer extends OptionalLongDeserializer { | ||
|
||
private OptionalLongAsStringDeserializer() {} | ||
|
||
@Override | ||
protected CoercionAction _checkFromStringCoercion(DeserializationContext _ctxt, String _value) { | ||
return CoercionAction.TryConvert; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters