forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Maintenance-2930] Review use of JSON Flattener (opensearch-project#3674
) ### Description Implement JsonFlattener helper class as written in opensearch-project#2926 to deprecate the use of the unnecessary JsonFlattener third party module. * Category (Enhancement, New feature, Bug fix, Test fix, Refactoring, Maintenance, Documentation) Maintenance * Why these changes are required? The JsonFlattener module was being utilized in only one place for one specific purpose, so these functions can be implemented as part of the OpenSearch codebase instead of importing an unnecessary third party module. * What is the old behavior before changes and new behavior after changes? Hopefully nothing. ### Issues Resolved - opensearch-project#2930 Is this a backport? If so, please add backport PR # and/or commits # No ### Testing Tests checked to make sure functions are not broken. ### Check List - [x] New functionality includes testing - [x] New functionality has been documented - [x] Commits are signed per the DCO using --signoff By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. For more information on following Developer Certificate of Origin and signing off your commits, please check [here](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin). --------- Signed-off-by: Prabhas Kurapati <[email protected]>
- Loading branch information
Showing
4 changed files
with
471 additions
and
9 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
66 changes: 66 additions & 0 deletions
66
src/main/java/org/opensearch/security/support/JsonFlattener.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,66 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
*/ | ||
|
||
package org.opensearch.security.support; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
|
||
import org.opensearch.core.common.Strings; | ||
import org.opensearch.security.DefaultObjectMapper; | ||
|
||
public class JsonFlattener { | ||
|
||
public static Map<String, Object> flattenAsMap(String jsonString) { | ||
try { | ||
final TypeReference<Map<String, Object>> typeReference = new TypeReference<>() { | ||
}; | ||
final Map<String, Object> jsonMap = DefaultObjectMapper.objectMapper.readValue(jsonString, typeReference); | ||
final Map<String, Object> flattenMap = new LinkedHashMap<>(); | ||
flattenEntries("", jsonMap.entrySet(), flattenMap); | ||
return flattenMap; | ||
} catch (final IOException ioe) { | ||
throw new IllegalArgumentException("Unparseable json", ioe); | ||
} | ||
} | ||
|
||
private static void flattenEntries(String prefix, final Iterable<Map.Entry<String, Object>> entries, final Map<String, Object> result) { | ||
if (!Strings.isNullOrEmpty(prefix)) { | ||
prefix += "."; | ||
} | ||
|
||
for (final Map.Entry<String, Object> e : entries) { | ||
flattenElement(prefix.concat(e.getKey()), e.getValue(), result); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static void flattenElement(String prefix, final Object source, final Map<String, Object> result) { | ||
if (source instanceof Iterable) { | ||
flattenCollection(prefix, (Iterable<Object>) source, result); | ||
} | ||
if (source instanceof Map) { | ||
flattenEntries(prefix, ((Map<String, Object>) source).entrySet(), result); | ||
} | ||
result.put(prefix, source); | ||
} | ||
|
||
private static void flattenCollection(String prefix, final Iterable<Object> objects, final Map<String, Object> result) { | ||
int counter = 0; | ||
for (final Object o : objects) { | ||
flattenElement(prefix + "[" + counter + "]", o, result); | ||
counter++; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.