Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.common.compatibility;

/**
* A enum representing versions which are used by a REST Compatible API.
* A CURRENT instance, represents a major Version.CURRENT from server module.
*
* Only major versions are supported.
*/
public enum RestApiCompatibleVersion {

V_8(8),
V_7(7);

public byte major;
private static RestApiCompatibleVersion CURRENT = V_8;

RestApiCompatibleVersion(int major) {
this.major = (byte) major;
}

public RestApiCompatibleVersion previousMajor() {
return fromMajorVersion(major - 1);
}

public static RestApiCompatibleVersion fromMajorVersion(int majorVersion) {
return valueOf("V_" + majorVersion);
}

public static RestApiCompatibleVersion minimumSupported() {
return currentVersion().previousMajor();
}

public static RestApiCompatibleVersion currentVersion() {
return CURRENT;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.elasticsearch.common.CheckedFunction;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -138,7 +139,8 @@ private Map<Class<?>, Map<String, Entry>> getRegistry(List<Entry> entries){
*/
public <T, C> T parseNamedObject(Class<T> categoryClass, String name, XContentParser parser, C context) throws IOException {

Map<String, Entry> parsers = parser.useCompatibility() ? compatibleRegistry.get(categoryClass) : registry.get(categoryClass);
Map<String, Entry> parsers = parser.getRestApiCompatibleVersion() == RestApiCompatibleVersion.minimumSupported() ?
compatibleRegistry.get(categoryClass) : registry.get(categoryClass);
if (parsers == null) {
if (registry.isEmpty()) {
// The "empty" registry will never work so we throw a better exception as a hint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.elasticsearch.common.xcontent;

import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -76,9 +78,10 @@ XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationH

/**
* Creates a parser over the provided input stream and with the indication that a request is using REST compatible API.
* Parses XContent using the N-1 compatible logic.
* Depending on restApiCompatibleVersionParses
* @param restApiCompatibleVersion - indicates if the N-1 or N compatible XContent parsing logic will be used.
*/
XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler,
InputStream is) throws IOException;
InputStream is, RestApiCompatibleVersion restApiCompatibleVersion) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.elasticsearch.common.xcontent;

import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.Flushable;
Expand Down Expand Up @@ -155,7 +157,7 @@ public interface HumanReadableTransformer {
*/
private boolean humanReadable = false;

private byte compatibleMajorVersion;
private RestApiCompatibleVersion restApiCompatibilityVersion;

private ParsedMediaType responseContentType;

Expand Down Expand Up @@ -1006,21 +1008,21 @@ public XContentBuilder copyCurrentStructure(XContentParser parser) throws IOExce

/**
* Sets a version used for serialising a response compatible with a previous version.
* @param restApiCompatibleVersion - indicates requested a version of API that the builder will be creating
*/
public XContentBuilder withCompatibleMajorVersion(byte compatibleMajorVersion) {
assert this.compatibleMajorVersion == 0 : "Compatible version has already been set";
if (compatibleMajorVersion == 0) {
throw new IllegalArgumentException("Compatible major version must not be equal to 0");
}
this.compatibleMajorVersion = compatibleMajorVersion;
public XContentBuilder withCompatibleVersion(RestApiCompatibleVersion restApiCompatibleVersion) {
assert this.restApiCompatibilityVersion == null : "restApiCompatibleVersion has already been set";
Objects.requireNonNull(restApiCompatibleVersion, "restApiCompatibleVersion cannot be null");
this.restApiCompatibilityVersion = restApiCompatibleVersion;
return this;
}

/**
* Returns a version used for serialising a response compatible with a previous version.
* Returns a version used for serialising a response.
* @return a compatible version
*/
public byte getCompatibleMajorVersion() {
return compatibleMajorVersion;
public RestApiCompatibleVersion getRestApiCompatibilityVersion() {
return restApiCompatibilityVersion;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.common.xcontent;

import org.elasticsearch.common.CheckedFunction;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -251,7 +252,7 @@ <T> Map<String, T> map(

boolean isClosed();

boolean useCompatibility();
RestApiCompatibleVersion getRestApiCompatibleVersion();

/**
* The callback to notify when parsing encounters a deprecated field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.common.xcontent;

import org.elasticsearch.common.CheckedFunction;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;

import java.io.IOException;
import java.nio.CharBuffer;
Expand Down Expand Up @@ -259,8 +260,8 @@ public boolean isClosed() {
}

@Override
public boolean useCompatibility() {
return parser.useCompatibility();
public RestApiCompatibleVersion getRestApiCompatibleVersion() {
return parser.getRestApiCompatibleVersion();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
Expand Down Expand Up @@ -98,9 +99,10 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,

@Override
public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, InputStream is)
DeprecationHandler deprecationHandler, InputStream is,
RestApiCompatibleVersion restApiCompatibleVersion)
throws IOException {
return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(is), true);
return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(is), restApiCompatibleVersion);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.common.xcontent.cbor;

import com.fasterxml.jackson.core.JsonParser;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentType;
Expand All @@ -22,8 +23,9 @@ public CborXContentParser(NamedXContentRegistry xContentRegistry,
}

public CborXContentParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, JsonParser parser, boolean useCompatibility) {
super(xContentRegistry, deprecationHandler, parser, useCompatibility);
DeprecationHandler deprecationHandler, JsonParser parser,
RestApiCompatibleVersion restApiCompatibleVersion) {
super(xContentRegistry, deprecationHandler, parser, restApiCompatibleVersion);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
Expand Down Expand Up @@ -99,8 +100,9 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,

@Override
public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, InputStream is) throws IOException {
return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(is), true);
DeprecationHandler deprecationHandler, InputStream is,
RestApiCompatibleVersion restApiCompatibleVersion) throws IOException {
return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(is), restApiCompatibleVersion);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentLocation;
Expand All @@ -27,13 +28,14 @@ public class JsonXContentParser extends AbstractXContentParser {

public JsonXContentParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, JsonParser parser) {
super(xContentRegistry, deprecationHandler, false);
super(xContentRegistry, deprecationHandler, RestApiCompatibleVersion.currentVersion());
this.parser = parser;
}

public JsonXContentParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, JsonParser parser, boolean useCompatibility) {
super(xContentRegistry, deprecationHandler, useCompatibility);
DeprecationHandler deprecationHandler, JsonParser parser,
RestApiCompatibleVersion restApiCompatibleVersion) {
super(xContentRegistry, deprecationHandler, restApiCompatibleVersion);
this.parser = parser;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
Expand Down Expand Up @@ -100,7 +101,8 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,

@Override
public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, InputStream is) throws IOException {
return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(is), true);
DeprecationHandler deprecationHandler, InputStream is,
RestApiCompatibleVersion restApiCompatibleVersion) throws IOException {
return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(is), restApiCompatibleVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.common.xcontent.smile;

import com.fasterxml.jackson.core.JsonParser;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentType;
Expand All @@ -22,8 +23,9 @@ public SmileXContentParser(NamedXContentRegistry xContentRegistry,
}

public SmileXContentParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, JsonParser parser, boolean useCompatibility) {
super(xContentRegistry, deprecationHandler, parser, useCompatibility);
DeprecationHandler deprecationHandler, JsonParser parser,
RestApiCompatibleVersion restApiCompatibleVersion) {
super(xContentRegistry, deprecationHandler, parser, restApiCompatibleVersion);
}

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

import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.CheckedFunction;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParseException;
Expand Down Expand Up @@ -46,16 +47,17 @@ private static void checkCoerceString(boolean coerce, Class<? extends Number> cl

private final NamedXContentRegistry xContentRegistry;
private final DeprecationHandler deprecationHandler;
private final boolean useCompatibility;
private final RestApiCompatibleVersion restApiCompatibleVersion;

public AbstractXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, boolean useCompatibility) {
public AbstractXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler,
RestApiCompatibleVersion restApiCompatibleVersion) {
this.xContentRegistry = xContentRegistry;
this.deprecationHandler = deprecationHandler;
this.useCompatibility = useCompatibility;
this.restApiCompatibleVersion = restApiCompatibleVersion;
}

public AbstractXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler) {
this(xContentRegistry, deprecationHandler, false);
this(xContentRegistry, deprecationHandler, RestApiCompatibleVersion.currentVersion());
}

// The 3rd party parsers we rely on are known to silently truncate fractions: see
Expand Down Expand Up @@ -413,8 +415,8 @@ public NamedXContentRegistry getXContentRegistry() {
public abstract boolean isClosed();

@Override
public boolean useCompatibility() {
return useCompatibility;
public RestApiCompatibleVersion getRestApiCompatibleVersion() {
return restApiCompatibleVersion;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
Expand Down Expand Up @@ -93,8 +94,9 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,

@Override
public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, InputStream is) throws IOException {
return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(is), true);
DeprecationHandler deprecationHandler, InputStream is,
RestApiCompatibleVersion restApiCompatibleVersion) throws IOException {
return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(is), restApiCompatibleVersion);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.common.xcontent.yaml;

import com.fasterxml.jackson.core.JsonParser;
import org.elasticsearch.common.compatibility.RestApiCompatibleVersion;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentType;
Expand All @@ -22,8 +23,9 @@ public YamlXContentParser(NamedXContentRegistry xContentRegistry,
}

public YamlXContentParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, JsonParser parser, boolean useCompatibility) {
super(xContentRegistry, deprecationHandler, parser, useCompatibility);
DeprecationHandler deprecationHandler, JsonParser parser,
RestApiCompatibleVersion restApiCompatibleVersion) {
super(xContentRegistry, deprecationHandler, parser, restApiCompatibleVersion);
}

@Override
Expand Down
Loading