Skip to content
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

Do not use ObjectMapper directly #21867

Closed
wants to merge 1 commit into from

Conversation

wendigo
Copy link
Contributor

@wendigo wendigo commented May 8, 2024

It doesn't have the configuration that we need

Follow-up to #21356

It doesn't have the configuration that we need

private static final int MAX_JSON_LENGTH_IN_ERROR_MESSAGE = 10_000;

// Note: JsonFactory is mutable, instances cannot be shared openly.
public static JsonFactory createJsonFactory()
{
return jsonFactoryBuilder().disable(CANONICALIZE_FIELD_NAMES).build();
return jsonFactoryBuilder().disable(CANONICALIZE_FIELD_NAMES).build().setCodec(OBJECT_MAPPED_UNORDERED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't use OBJECT_MAPPED_UNORDERED before. Why this change?

@@ -151,7 +151,7 @@ private static <T> T parseJson(JsonNode node, String jsonPointer, Class<T> javaT

public static JsonFactory jsonFactory()
{
return jsonFactoryBuilder().build();
return jsonFactoryBuilder().build().setCodec(OBJECT_MAPPER);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this break encapsulation of OBJECT_MAPPER?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it indeed does:

((ObjectMapper) jsonFactory().getCodec()).configure(ORDER_MAP_ENTRIES_BY_KEYS, false)

@@ -151,7 +151,7 @@ private static <T> T parseJson(JsonNode node, String jsonPointer, Class<T> javaT

public static JsonFactory jsonFactory()
{
return jsonFactoryBuilder().build();
return jsonFactoryBuilder().build().setCodec(OBJECT_MAPPER);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it indeed does:

((ObjectMapper) jsonFactory().getCodec()).configure(ORDER_MAP_ENTRIES_BY_KEYS, false)

@wendigo
Copy link
Contributor Author

wendigo commented May 8, 2024

@findepi Is first commit ok? I'll drop the second one.

@wendigo wendigo force-pushed the serafin/use-object-mapper-provider branch from 428bf14 to 0ab046d Compare May 8, 2024 14:46
@@ -56,7 +59,13 @@ public final class JsonInputFunctions
public static final String VARBINARY_UTF16_TO_JSON = "$varbinary_utf16_to_json";
public static final String VARBINARY_UTF32_TO_JSON = "$varbinary_utf32_to_json";

private static final ObjectMapper MAPPER = new ObjectMapper();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if new ObjectMapper() isn't supposed to be called, let's flag it out with eg modernizer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's called in a couple of places (i.e. tests) and in most of them it's harmless but in the engine code it should be banned because it operates on both user input and these instances live long.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. so how do we prevent new not harmless direct uses of ObjectMapper?


static {
// Changes factory. Necessary for JsonParser.readValueAsTree to work.
new ObjectMapper(JSON_FACTORY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have to switch to json factory?
or would using new ObjectMapperProvider().get() (instead of new ObjectMapper()) be enough?

Comment on lines +67 to +69
static {
// Changes factory. Necessary for JsonParser.readValueAsTree to work.
new ObjectMapper(JSON_FACTORY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code here does not call readValueAsTree()
is the static block redundant?

@findepi
Copy link
Member

findepi commented May 8, 2024

in the commit message there is explanation:

It doesn't have the configuration that we need

i think a reader would naturally ask -- what was the configuration that we need that was missing.

@wendigo wendigo closed this May 10, 2024
@findepi findepi deleted the serafin/use-object-mapper-provider branch May 10, 2024 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants