-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Introduce Compatible Version plugin #64481
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
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
e468fdf
Introduce per endpoint media types
pgomulka 5cfa11e
Merge branch 'master' into compat/parsed_media_type
pgomulka 66224e7
allow smile and cbor to parse charset param. See ClientYamlTestExecut…
pgomulka 29b27e8
javadocs
pgomulka 4690362
fix javadoc
pgomulka e7866a9
Allow registration of compatible version-1 handlers
pgomulka 0330d97
add tests
pgomulka d3cb858
Introduce CompatibleVersion plugin
pgomulka 78d4660
Merge branch 'master' into compat/register_compatible_handlers
pgomulka bc5de8b
pass version to xcontentbuilder
pgomulka f958cdd
Merge branch 'master' into compat/register_compatible_handlers
pgomulka e829b17
Merge branch 'master' into compat/compatible_plugin
pgomulka f32cf70
spotless
pgomulka 2bda74f
code review follow up
pgomulka 7779083
minor tweaks
pgomulka dd26408
Merge branch 'master' into compat/introduce_per_endpoint_media_types
pgomulka 94b4a0a
fix test after exception msg rename
pgomulka 4385591
rename to header value
pgomulka dc61731
remove charset validation
pgomulka 4a3138d
Apply suggestions from code review
pgomulka 0b565e5
javadoc
pgomulka b35ad2c
Merge branch 'master' into compat/introduce_per_endpoint_media_types
pgomulka b908bfe
Merge branch 'compat/introduce_per_endpoint_media_types' of github.co…
pgomulka 950ba7b
Merge branch 'compat/introduce_per_endpoint_media_types' into compat/…
pgomulka 310b735
Merge branch 'master' into compat/register_compatible_handlers
pgomulka 3cf408a
Merge branch 'compat/introduce_per_endpoint_media_types' into compat/…
pgomulka 4c880c0
Merge branch 'master' into compat/compatible_plugin
pgomulka afba70d
javadoc and cleanup
pgomulka 385f5a9
Merge branch 'master' into compat/register_compatible_handlers
pgomulka 836fe0f
tests and javadoc
pgomulka 8f2ea92
javadoc
pgomulka d70a071
do not set compatible version twice
pgomulka 87d762f
Merge branch 'master' into compat/register_compatible_handlers
pgomulka deff739
javadocs
pgomulka ee40697
Merge branch 'compat/register_compatible_handlers' into compat/compat…
pgomulka a8eb296
Merge branch 'master' into compat/compatible_plugin
pgomulka 63c2987
line
pgomulka aab691b
testcase for textformat versioned
pgomulka b2e12ec
spotless
pgomulka 16a1b83
minimum rest compat
pgomulka b75d415
code review follow up
pgomulka c3b7b51
fix messages and toString for parsedmediatype
pgomulka 51de734
assertion on message in test
pgomulka 445032c
Merge branch 'master' into compat/compatible_plugin
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
40 changes: 40 additions & 0 deletions
40
server/src/main/java/org/elasticsearch/plugins/RestCompatibilityPlugin.java
This file contains hidden or 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,40 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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. | ||
| */ | ||
|
|
||
| package org.elasticsearch.plugins; | ||
|
|
||
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.common.Nullable; | ||
| import org.elasticsearch.common.xcontent.ParsedMediaType; | ||
|
|
||
|
|
||
| /** | ||
| * An extension point for Compatible API plugin implementation. | ||
| */ | ||
| public interface RestCompatibilityPlugin { | ||
| /** | ||
| * Returns a version which was requested on Accept and Content-Type headers | ||
| * | ||
| * @param acceptHeader - a ParsedMediaType parsed from Accept header | ||
| * @param contentTypeHeader - a ParsedMediaType parsed from Content-Type header | ||
| * @param hasContent - a flag indicating if a request has content | ||
| * @return a requested Compatible API Version | ||
| */ | ||
| Version getCompatibleVersion(@Nullable ParsedMediaType acceptHeader, @Nullable ParsedMediaType contentTypeHeader, boolean hasContent); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,32 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch 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. | ||
| */ | ||
|
|
||
| apply plugin: 'elasticsearch.esplugin' | ||
|
|
||
| esplugin { | ||
| name 'rest-compatibility' | ||
| description 'A plugin for Compatible Rest API' | ||
| classname 'org.elasticsearch.compat.CompatibleVersionPlugin' | ||
| extendedPlugins = ['x-pack-core'] | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly project(path: xpackModule('core'), configuration: 'default') | ||
| testImplementation project(path: xpackModule('core'), configuration: 'testArtifacts') | ||
| } |
96 changes: 96 additions & 0 deletions
96
...in/rest-compatibility/src/main/java/org/elasticsearch/compat/CompatibleVersionPlugin.java
This file contains hidden or 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,96 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| package org.elasticsearch.compat; | ||
|
|
||
| import org.elasticsearch.ElasticsearchStatusException; | ||
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.common.Nullable; | ||
| import org.elasticsearch.common.xcontent.MediaType; | ||
| import org.elasticsearch.common.xcontent.ParsedMediaType; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.plugins.RestCompatibilityPlugin; | ||
| import org.elasticsearch.rest.RestStatus; | ||
|
|
||
| public class CompatibleVersionPlugin extends Plugin implements RestCompatibilityPlugin { | ||
|
|
||
| @Override | ||
| public Version getCompatibleVersion( | ||
| @Nullable ParsedMediaType acceptHeader, | ||
| @Nullable ParsedMediaType contentTypeHeader, | ||
| boolean hasContent | ||
| ) { | ||
| Byte aVersion = parseVersion(acceptHeader); | ||
| byte acceptVersion = aVersion == null ? Version.CURRENT.major : Integer.valueOf(aVersion).byteValue(); | ||
| Byte cVersion = parseVersion(contentTypeHeader); | ||
| byte contentTypeVersion = cVersion == null ? Version.CURRENT.major : Integer.valueOf(cVersion).byteValue(); | ||
|
|
||
| // accept version must be current or prior | ||
| if (acceptVersion > Version.CURRENT.major || acceptVersion < Version.CURRENT.minimumRestCompatibilityVersion().major) { | ||
| throw new ElasticsearchStatusException( | ||
| "Accept version must be either version {} or {}, but found {}. Accept={}", | ||
| RestStatus.BAD_REQUEST, | ||
| Version.CURRENT.major, | ||
| Version.CURRENT.minimumRestCompatibilityVersion().major, | ||
| acceptVersion, | ||
| acceptHeader | ||
| ); | ||
| } | ||
| if (hasContent) { | ||
|
|
||
| // content-type version must be current or prior | ||
| if (contentTypeVersion > Version.CURRENT.major | ||
| || contentTypeVersion < Version.CURRENT.minimumRestCompatibilityVersion().major) { | ||
| throw new ElasticsearchStatusException( | ||
| "Content-Type version must be either version {} or {}, but found {}. Content-Type={}", | ||
| RestStatus.BAD_REQUEST, | ||
| Version.CURRENT.major, | ||
| Version.CURRENT.minimumRestCompatibilityVersion().major, | ||
| contentTypeVersion, | ||
| contentTypeHeader | ||
| ); | ||
| } | ||
| // if both accept and content-type are sent, the version must match | ||
| if (contentTypeVersion != acceptVersion) { | ||
| throw new ElasticsearchStatusException( | ||
| "A compatible version is required on both Content-Type and Accept headers " | ||
| + "if either one has requested a compatible version " | ||
| + "and the compatible versions must match. Accept={}, Content-Type={}", | ||
| RestStatus.BAD_REQUEST, | ||
| acceptHeader, | ||
| contentTypeHeader | ||
| ); | ||
| } | ||
| // both headers should be versioned or none | ||
| if ((cVersion == null && aVersion != null) || (aVersion == null && cVersion != null)) { | ||
| throw new ElasticsearchStatusException( | ||
| "A compatible version is required on both Content-Type and Accept headers " | ||
| + "if either one has requested a compatible version. Accept={}, Content-Type={}", | ||
| RestStatus.BAD_REQUEST, | ||
| acceptHeader, | ||
| contentTypeHeader | ||
| ); | ||
| } | ||
| if (contentTypeVersion < Version.CURRENT.major) { | ||
| return Version.CURRENT.previousMajor(); | ||
| } | ||
| } | ||
|
|
||
| if (acceptVersion < Version.CURRENT.major) { | ||
| return Version.CURRENT.previousMajor(); | ||
| } | ||
|
|
||
| return Version.CURRENT; | ||
| } | ||
|
|
||
| // scope for testing | ||
| static Byte parseVersion(ParsedMediaType parsedMediaType) { | ||
| if (parsedMediaType != null) { | ||
| String version = parsedMediaType.getParameters().get(MediaType.COMPATIBLE_WITH_PARAMETER_NAME); | ||
| return version != null ? Byte.parseByte(version) : null; | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this ever evaluate true ? wouldn't the above if catch any this case ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the above if
if (contentTypeVersion != acceptVersion) {would not catch the scenario when one of the headers is set tocompatible-with=8(current in general) and the other is not set (defaults to current)