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

Fix pattern replace by making flag optional as on api #895

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ This section is for maintaining a changelog for all breaking changes for the cli

### Fixed
- Fix missing properties on UpdateOperation ([#744](https://github.com/opensearch-project/opensearch-java/pull/744))
- Fix pattern replace by making flag optional as on api ([#895](https://github.com/opensearch-project/opensearch-java/pull/895))
grouh marked this conversation as resolved.
Show resolved Hide resolved

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import jakarta.json.stream.JsonGenerator;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
Expand All @@ -46,6 +47,7 @@

@JsonpDeserializable
public class PatternReplaceCharFilter extends CharFilterBase implements CharFilterDefinitionVariant {
@Nullable
private final String flags;

private final String pattern;
Expand All @@ -57,7 +59,7 @@ public class PatternReplaceCharFilter extends CharFilterBase implements CharFilt
private PatternReplaceCharFilter(Builder builder) {
super(builder);

this.flags = ApiTypeHelper.requireNonNull(builder.flags, this, "flags");
this.flags = builder.flags;
this.pattern = ApiTypeHelper.requireNonNull(builder.pattern, this, "pattern");
this.replacement = ApiTypeHelper.requireNonNull(builder.replacement, this, "replacement");

Expand All @@ -76,8 +78,9 @@ public CharFilterDefinition.Kind _charFilterDefinitionKind() {
}

/**
* Required - API name: {@code flags}
* API name: {@code flags}
*/
@Nullable
public final String flags() {
return this.flags;
}
Expand All @@ -100,8 +103,11 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

generator.write("type", "pattern_replace");
super.serializeInternal(generator, mapper);
generator.writeKey("flags");
generator.write(this.flags);

if (this.flags != null) {
generator.writeKey("flags");
generator.write(this.flags);
}

generator.writeKey("pattern");
generator.write(this.pattern);
Expand All @@ -118,16 +124,17 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
*/

public static class Builder extends CharFilterBase.AbstractBuilder<Builder> implements ObjectBuilder<PatternReplaceCharFilter> {
@Nullable
private String flags;

private String pattern;

private String replacement;

/**
* Required - API name: {@code flags}
* API name: {@code flags}
*/
public final Builder flags(String value) {
public final Builder flags(@Nullable String value) {
this.flags = value;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.client.opensearch._types.analysis;

import static org.junit.Assert.assertEquals;

import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import org.junit.Test;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;

public class PatternReplaceCharFilterTest {
@Test
public void testCreatePatternReplaceCharFilter() {
PatternReplaceCharFilter patternReplaceCharFilter = new PatternReplaceCharFilter.Builder().pattern("pattern")
.replacement("replacement")
.build();
assertEquals("pattern", patternReplaceCharFilter.pattern());
assertEquals("replacement", patternReplaceCharFilter.replacement());
}

@Test
public void testCreatePatternReplaceCharFilterWithFlags() {
PatternReplaceCharFilter patternReplaceCharFilter = new PatternReplaceCharFilter.Builder().pattern("pattern")
.replacement("replacement")
.flags("flags")
.build();
assertEquals("pattern", patternReplaceCharFilter.pattern());
assertEquals("replacement", patternReplaceCharFilter.replacement());
assertEquals("flags", patternReplaceCharFilter.flags());
}

@Test
public void testDeserializePatternReplaceCharFilter() {
String jsonString =
"{\"type\": \"pattern_replace\", \"pattern\": \"pattern\", \"replacement\": \"replacement\", \"flags\": \"flags\"}";

StringReader reader = new StringReader(jsonString);
JacksonJsonpMapper mapper = new JacksonJsonpMapper();
JsonParser parser = mapper.jsonProvider().createParser(reader);

PatternReplaceCharFilter patternReplaceCharFilter = PatternReplaceCharFilter._DESERIALIZER.deserialize(parser, mapper);
assertEquals("pattern", patternReplaceCharFilter.pattern());
assertEquals("replacement", patternReplaceCharFilter.replacement());
assertEquals("flags", patternReplaceCharFilter.flags());
}
}
Loading