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

feature-2774 webhook connector can now handle json array in request body #2803

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

mathias-vandaele
Copy link
Contributor

Description

The function which was handling the deserialization of the body from in incoming request could only create a map, if a array of primitive or object was passed, an exception was raised.

This function was called in many places, and something would be casted into a Map<String, Object> Sometime into an Object

In order to manage current and future feature. I created 3 wrapping classes that all hold the body in its different forms:

  • Map<String, Object>
  • List<Map<String, Object>>
  • List<Object>

They all Implement the RawWebHookBody that holds all the methods that can interact with the body

Related issues

closes #2774

@sbuettner
Copy link
Contributor

@mathias-vandaele Thanks for tackling this. I think we can solve is issue in potentially simpler way. The expected type of the MappedHttpRequest.body property is Object. We can therefore simply map the incoming request body to Object via Jackson without any custom serialization.

@mathias-vandaele mathias-vandaele force-pushed the 2774-webhook-connector-cannot-handle-json-array-in-request-body branch from aa0f440 to 1970216 Compare June 27, 2024 08:26
@mathias-vandaele
Copy link
Contributor Author

mathias-vandaele commented Jun 27, 2024

@mathias-vandaele Thanks for tackling this. I think we can solve is issue in potentially simpler way. The expected type of the MappedHttpRequest.body property is Object. We can therefore simply map the incoming request body to Object via Jackson without any custom serialization.

@sbuettner I made a way simpler solution :D

Comment on lines 43 to 48
private static <T> Map<T, T> checkedCastToMap(Object o, Class<T> tClass) {
if (o instanceof Map<?, ?> map) {
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (!(tClass.isInstance(entry.getValue())) || !(tClass.isInstance(entry.getKey()))) {
return Map.of();
}
}
return (Map<T, T>) map;
}
return Map.of();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

As we cant calculate the signature of an empty map anyway. What about skipping it when the payload is not of type map?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we have a String[] couldn't we use the values to calculate the signature?
I don't know where it is used, so I might be wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As we cant calculate the signature of an empty map anyway. What about skipping it when the payload is not of type map?

Yes let me refactor it to match this behavior

If we have a String[] couldn't we use the values to calculate the signature? I don't know where it is used, so I might be wrong

@johnBgood That would be easy to do, but I don't have the big picture for this feature, can it be send as an array? but yes I though about it at first

Copy link
Contributor

Choose a reason for hiding this comment

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

Not send as an array, but we could build the signature in a similar way of what we do with Maps, basically concatenating values

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the given list would be in this format ["key1", "value1", "key2", "value2"] @johnBgood ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes exactly :) Conceptually it would be more ["value1", "value2"...] but that's the spirit yes.
Instead of adding keys/values from a Map (see below), we would simply add the array's values.

for (String key : sortedKeys) {
      builder.append(key);
      String value = signatureData.get(key);
      builder.append(value == null ? "" : value);
    }

Copy link
Contributor

@johnBgood johnBgood left a comment

Choose a reason for hiding this comment

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

Just a few comments :)

for (String key : sortedKeys) {
builder.append(key);
String value = signatureData.get(key);
builder.append(value == null ? "" : value);
}
return builder.toString();
}

private static <T> Map<T, T> checkedCastToMap(Object o, Class<T> tClass) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we don't have any other use cases than the tClass being a String. I'd suggest removing the generics then for visibility

for (String key : sortedKeys) {
builder.append(key);
String value = signatureData.get(key);
builder.append(value == null ? "" : value);
}
return builder.toString();
}

private static <T> Map<T, T> checkedCastToMap(Object o, Class<T> tClass) {
if (o instanceof Map<?, ?> map) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if this is true do we really have to iterate over the entrySet?

Comment on lines 43 to 48
private static <T> Map<T, T> checkedCastToMap(Object o, Class<T> tClass) {
if (o instanceof Map<?, ?> map) {
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (!(tClass.isInstance(entry.getValue())) || !(tClass.isInstance(entry.getKey()))) {
return Map.of();
}
}
return (Map<T, T>) map;
}
return Map.of();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If we have a String[] couldn't we use the values to calculate the signature?
I don't know where it is used, so I might be wrong

@mathias-vandaele mathias-vandaele force-pushed the 2774-webhook-connector-cannot-handle-json-array-in-request-body branch from 68200b9 to 1370169 Compare July 2, 2024 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Webhook connector cannot handle json array in request body
3 participants