Skip to content

Commit

Permalink
Fix ScoreFunction: variants are optional (#752)
Browse files Browse the repository at this point in the history
Signed-off-by: 西狩 <923568791@qq.com>
  • Loading branch information
Huiming Li authored and lihuimingxs committed Dec 28, 2023
1 parent 5239303 commit 00eb575
Show file tree
Hide file tree
Showing 21 changed files with 1,359 additions and 347 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Fixed
- Fix partial success results for msearch_template ([#709](https://github.com/opensearch-project/opensearch-java/pull/709))
- Fix deserialization of node stats response ([#745](https://github.com/opensearch-project/opensearch-java/pull/745))
- Fix ScoreFunction: variants are optional ([#752](https://github.com/opensearch-project/opensearch-java/pull/752))

### Security

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.client.json;

import jakarta.json.spi.JsonProvider;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import javax.annotation.Nullable;

public abstract class DelegatingJsonpMapper implements JsonpMapper {

protected final JsonpMapper mapper;

public DelegatingJsonpMapper(JsonpMapper mapper) {
this.mapper = mapper;
}

@Override
public JsonProvider jsonProvider() {
return mapper.jsonProvider();
}

@Override
public <T> T deserialize(JsonParser parser, Class<T> clazz) {
return mapper.deserialize(parser, clazz);
}

@Override
public <T> void serialize(T value, JsonGenerator generator) {
mapper.serialize(value, generator);
}

@Override
public boolean ignoreUnknownFields() {
return mapper.ignoreUnknownFields();
}

@Override
@Nullable
public <T> T attribute(String name) {
return mapper.attribute(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,42 @@
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;

public abstract class JsonpMapperBase implements JsonpMapper {

/** Get a serializer when none of the builtin ones are applicable */
protected abstract <T> JsonpDeserializer<T> getDefaultDeserializer(Class<T> clazz);

private Map<String, Object> attributes;

@Nullable
@Override
@SuppressWarnings("unchecked")
public <T> T attribute(String name) {
return attributes == null ? null : (T) attributes.get(name);
}

/**
* Updates attributes to a copy of the current ones with an additional key/value pair.
*
* Mutates the current mapper, intended to be used in implementations of {@link #withAttribute(String, Object)}
*/
protected JsonpMapperBase addAttribute(String name, Object value) {
if (attributes == null) {
this.attributes = Collections.singletonMap(name, value);
} else {
Map<String, Object> newAttrs = new HashMap<>(attributes.size() + 1);
newAttrs.putAll(attributes);
newAttrs.put(name, value);
this.attributes = newAttrs;
}
return this;
}

@Override
public <T> T deserialize(JsonParser parser, Class<T> clazz) {
JsonpDeserializer<T> deserializer = findDeserializer(clazz);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.client.json;

/**
* Defines attribute names for {@link JsonpMapper} features.
*/
public class JsonpMapperFeatures {

/**
*
*/
public static final String SERIALIZE_TYPED_KEYS = JsonpMapperFeatures.class.getName() + ":SERIALIZE_TYPED_KEYS";

/**
* Disables custom variants in union types (false by default). Used in tests to avoid interpreting wrong variant kinds as a custom
* extension.
*/
public static final String FORBID_CUSTOM_VARIANTS = JsonpMapperFeatures.class.getName() + ":FORBID_CUSTOM_VARIANTS";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* 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.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.client.json;

import jakarta.json.stream.JsonLocation;
import jakarta.json.stream.JsonParser;
import jakarta.json.stream.JsonParsingException;
import java.util.LinkedList;
import java.util.regex.Pattern;

/**
* A mapping exception. The exception message contains the JSON path and location where the problem happened.
*/
public class JsonpMappingException extends JsonParsingException {
private final LinkedList<Object> path = new LinkedList<>();
private Object ref;

public JsonpMappingException(String message, JsonLocation location) {
super(message, location);
}

public JsonpMappingException(String message, Throwable cause, JsonLocation location) {
super(message, cause, location);
}

public JsonpMappingException(Throwable cause, JsonLocation location) {
super(cause.toString(), cause, location);
}

private static final Pattern identifier = Pattern.compile("[_a-zA-Z][_a-zA-Z0-9]*");

@Override
public String getMessage() {
StringBuilder sb = new StringBuilder("Error deserializing");
if (ref != null) {
sb.append(' ');
String className = ref.getClass().getName();
if (className.endsWith("$Builder")) {
sb.append(className, 0, className.length() - "$Builder".length());
} else {
sb.append(className);
}
}
sb.append(": ").append(super.getMessage());

if (!path.isEmpty()) {
sb.append(" (JSON path: ");
path(sb);
sb.append(") ");
}

sb.append(getLocation());
return sb.toString();
}

/**
* The JSON path where this exception happened.
*/
public String path() {
StringBuilder sb = new StringBuilder();
path(sb);
return sb.toString();
}

// Package-visible for testing
void path(StringBuilder sb) {
String sep = "";
for (Object item : path) {
if (item instanceof Integer) {
sb.append("[").append(((Integer) item).intValue()).append("]");
} else {
String str = item.toString();
if (identifier.matcher(str).matches()) {
sb.append(sep).append(item);
} else {
sb.append("['").append(str).append("']");
}
}
sep = ".";
}
}

public JsonpMappingException prepend(Object ref, String name) {
return prepend0(ref, name);
}

public JsonpMappingException prepend(Object ref, int idx) {
return prepend0(ref, idx);
}

private JsonpMappingException prepend0(Object ref, Object pathItem) {
if (pathItem != null) {
this.path.addFirst(pathItem);
}
// Keep the deepest object reference in the JSON hierarchy
if (this.ref == null) {
this.ref = ref;
}
return this;
}

public static JsonpMappingException from(Throwable cause, Object ref, String name, JsonParser parser) {
return from0(cause, ref, name, parser);
}

public static JsonpMappingException from(Throwable cause, int index, JsonParser parser) {
return from0(cause, null, index, parser);
}

private static JsonpMappingException from0(Throwable cause, Object ref, Object pathItem, JsonParser parser) {
JsonpMappingException jme;

if (cause instanceof JsonpMappingException) {
jme = (JsonpMappingException) cause;
} else {
jme = new JsonpMappingException(cause, parser.getLocation());
}

return jme.prepend0(ref, pathItem);
}
}

Loading

0 comments on commit 00eb575

Please sign in to comment.