ditto icon indicating copy to clipboard operation
ditto copied to clipboard

Add handling nested properties during resolving jwt placeholders

Open dimabarbul opened this issue 1 year ago • 3 comments

Resolves #1985

dimabarbul avatar Aug 19 '24 17:08 dimabarbul

@dimabarbul thanks a lot for the PR. Will review next week once back from my vacation.

thjaeckle avatar Aug 20 '24 08:08 thjaeckle

@thjaeckle Give me some time (couple of days or so), please, I want to make another version with HashSet instead of Stream for comparison. I haven't checked performance, but I'm pretty sure, HashSet version should be better.

dimabarbul avatar Aug 29 '24 03:08 dimabarbul

HashSet version is:

    private static Set<String> resolveValues(final JsonValue jsonValue, final JsonPointer jsonPointer) {
        final Set<String> result = new HashSet<>();

        resolveValuesInner(jsonValue, jsonPointer, result);

        return result;
    }

    private static void resolveValuesInner(
            final JsonValue jsonValue, final JsonPointer jsonPointer, final Set<String> result) {
        jsonPointer.getRoot()
                .ifPresentOrElse(
                        root -> {
                            if (jsonValue.isArray()) {
                                jsonValue.asArray().stream()
                                        .forEach(item -> resolveValuesInner(item, jsonPointer, result));
                            } else if (jsonValue.isObject()) {
                                jsonValue.asObject().getValue(root)
                                        .ifPresent(v -> resolveValuesInner(v, jsonPointer.nextLevel(), result));
                            }
                        },
                        () -> {
                            if (jsonValue.isArray()) {
                                jsonValue.asArray().stream().map(JsonValue::formatAsString).forEach(result::add);
                            } else {
                                result.add(jsonValue.formatAsString());
                            }
                        });
    }

I've roughly measured performance of HashSet and Stream versions (using System.currentTimeMillis) and found that time is relatively the same (with stream version slightly better). So I'll leave the PR as is.

dimabarbul avatar Aug 29 '24 04:08 dimabarbul