Skip to content

Commit

Permalink
Fixes a bug in toXContent
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Dec 31, 2024
1 parent 00ba7a7 commit 6fb5055
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (recipients.isEmpty()) {
return builder;
}
builder.startObject();
for (Map.Entry<RecipientType, Set<String>> entry : recipients.entrySet()) {
builder.array(entry.getKey().getType(), entry.getValue().toArray());
}
builder.endObject();
return builder;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.test.OpenSearchTestCase;
Expand Down Expand Up @@ -154,7 +155,10 @@ public void testFromXContentWithUnexpectedEndOfInput() throws IOException {
}

public void testToXContentBuildsCorrectly() throws IOException {
SharedWithScope scope = new SharedWithScope("scope1", new SharedWithScope.ScopeRecipients(Map.of()));
SharedWithScope scope = new SharedWithScope(
"scope1",
new SharedWithScope.ScopeRecipients(Map.of(new RecipientType("users"), Set.of("bleh")))
);

Set<SharedWithScope> scopes = new HashSet<>();
scopes.add(scope);
Expand All @@ -163,11 +167,11 @@ public void testToXContentBuildsCorrectly() throws IOException {

XContentBuilder builder = JsonXContent.contentBuilder();

shareWith.toXContent(builder, null);
shareWith.toXContent(builder, ToXContent.EMPTY_PARAMS);

String result = builder.toString();

String expected = "{\"scope1\":{}}";
String expected = "{\"scope1\":{\"users\":[\"bleh\"]}}";

MatcherAssert.assertThat(expected.length(), equalTo(result.length()));
MatcherAssert.assertThat(expected, equalTo(result));
Expand Down

0 comments on commit 6fb5055

Please sign in to comment.