From df2b1bdb0443b7870f32151225a2d10c2c593c4e Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Fri, 15 Nov 2024 10:18:10 +1300 Subject: [PATCH] Generate basic index template operations (#1293) * Generate basic index template operations Generate indices.get_index_template Signed-off-by: Thomas Farr Generate indices.put_index_template Signed-off-by: Thomas Farr Generate indices.exists_index_template Signed-off-by: Thomas Farr Generate indices.delete_index_template Signed-off-by: Thomas Farr Generate indices.simulate_index_template Signed-off-by: Thomas Farr * Allow deserializing path params from JSON Signed-off-by: Thomas Farr * Fix compile Signed-off-by: Thomas Farr --------- Signed-off-by: Thomas Farr (cherry picked from commit c7103d87423e44aa5ef57bb940496a6e3b74ff78) --- .../transport/util/FunnellingHttpsProxy.java | 2 +- .../integTest/AbstractIndicesClientIT.java | 3 +-- java-codegen/opensearch-openapi.yaml | 22 ++++++++++++++----- .../client/codegen/model/ObjectShape.java | 14 +++++++++++- .../client/codegen/model/RequestShape.java | 8 +++++++ .../codegen/templates/ObjectShape.mustache | 12 ++++++++++ .../ObjectShape/Deserialize.mustache | 4 ++-- .../templates/ObjectShape/Serialize.mustache | 4 ++-- .../codegen/templates/RequestShape.mustache | 18 ++++++++------- .../client/samples/DataStreamBasics.java | 3 +-- 10 files changed, 67 insertions(+), 23 deletions(-) diff --git a/java-client/src/test/java/org/opensearch/client/transport/util/FunnellingHttpsProxy.java b/java-client/src/test/java/org/opensearch/client/transport/util/FunnellingHttpsProxy.java index 19b62a7eb6..eef2416487 100644 --- a/java-client/src/test/java/org/opensearch/client/transport/util/FunnellingHttpsProxy.java +++ b/java-client/src/test/java/org/opensearch/client/transport/util/FunnellingHttpsProxy.java @@ -28,7 +28,7 @@ import org.apache.hc.core5.http.impl.EnglishReasonPhraseCatalog; public class FunnellingHttpsProxy implements Closeable { - private static final int SO_TIMEOUT = 5000; + private static final int SO_TIMEOUT = 10000; @Nonnull private final ServerSocket serverSocket; diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java index 11d7de218f..09747c666c 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java @@ -22,7 +22,6 @@ import org.opensearch.client.opensearch.indices.CreateDataStreamResponse; import org.opensearch.client.opensearch.indices.CreateIndexRequest; import org.opensearch.client.opensearch.indices.CreateIndexResponse; -import org.opensearch.client.opensearch.indices.DataStream; import org.opensearch.client.opensearch.indices.DataStreamsStatsResponse; import org.opensearch.client.opensearch.indices.DeleteDataStreamResponse; import org.opensearch.client.opensearch.indices.GetAliasRequest; @@ -127,7 +126,7 @@ public void testDataStream() throws IOException { PutIndexTemplateResponse putIndexTemplateResponse = javaClient().indices() .putIndexTemplate( b -> b.name(dataStreamIndexTemplateName) - .dataStream(new DataStream.Builder().timestampField(bd -> bd.name(timestampFieldName)).build()) + .dataStream(ds -> ds.timestampField(bd -> bd.name(timestampFieldName))) .indexPatterns(namePattern) ); assertTrue(putIndexTemplateResponse.acknowledged()); diff --git a/java-codegen/opensearch-openapi.yaml b/java-codegen/opensearch-openapi.yaml index 8dd8aab304..3d39385521 100644 --- a/java-codegen/opensearch-openapi.yaml +++ b/java-codegen/opensearch-openapi.yaml @@ -10210,8 +10210,8 @@ paths: requestBody: $ref: '#/components/requestBodies/create' responses: - '200': - $ref: '#/components/responses/create___200' + '201': + $ref: '#/components/responses/create___201' put: operationId: create.1 x-operation-group: create @@ -10240,8 +10240,8 @@ paths: requestBody: $ref: '#/components/requestBodies/create' responses: - '200': - $ref: '#/components/responses/create___200' + '201': + $ref: '#/components/responses/create___201' /{index}/_delete_by_query: post: operationId: delete_by_query.0 @@ -10415,6 +10415,8 @@ paths: responses: '200': $ref: '#/components/responses/exists___200' + '404': + $ref: '#/components/responses/exists___404' post: operationId: index.1 x-operation-group: index @@ -28037,7 +28039,7 @@ components: type: integer format: int64 description: '' - create___200: + create___201: content: application/json: schema: @@ -28137,6 +28139,10 @@ components: content: application/json: {} description: '' + exists___404: + content: + application/json: {} + description: '' explain___200: content: application/json: @@ -39275,6 +39281,12 @@ components: $ref: '#/components/schemas/_common.analysis___IcuCollationStrength' variable_top: type: string + country: + type: string + language: + type: string + variant: + type: string required: - type _common.mapping___IndexField: diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/model/ObjectShape.java b/java-codegen/src/main/java/org/opensearch/client/codegen/model/ObjectShape.java index 3cbabe8754..12fab62b95 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/model/ObjectShape.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/model/ObjectShape.java @@ -72,10 +72,18 @@ public void setShortcutProperty(String shortcutProperty) { this.shortcutProperty = shortcutProperty; } + public Collection getFieldsToSerialize() { + return getBodyFields(); + } + public boolean hasFieldsToSerialize() { return !bodyFields.isEmpty() || additionalPropertiesField != null; } + public Collection getFieldsToDeserialize() { + return getBodyFields(); + } + public Collection getReferencingDiscriminatedUnions() { return getIncomingReference(ReferenceKind.UnionVariant).stream() .map(s -> (TaggedUnionShape) s) @@ -119,7 +127,11 @@ public Collection getAnnotations() { } public boolean shouldImplementPlainDeserializable() { - return Set.of("SourceField", "TypeMapping").contains(getClassName()); + return Set.of("IndexTemplateMapping", "SourceField", "TypeMapping").contains(getClassName()); + } + + public boolean canBeSingleton() { + return bodyFields.isEmpty() && additionalPropertiesField == null && !extendsOtherShape(); } public static class ReferencingDiscriminatedUnion { diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/model/RequestShape.java b/java-codegen/src/main/java/org/opensearch/client/codegen/model/RequestShape.java index df8ffabe6f..0877974e78 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/model/RequestShape.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/model/RequestShape.java @@ -93,10 +93,18 @@ public Type getResponseType() { : Types.Client.Transport.Endpoints.BooleanResponse; } + @Override public boolean canBeSingleton() { return !hasRequestBody() && !hasQueryParams() && hasSinglePath() && !getFirstPath().hasParams(); } + @Override + public Collection getFieldsToDeserialize() { + var fields = new TreeMap<>(bodyFields); + fields.putAll(pathParams); + return fields.values(); + } + public boolean hasRequestBody() { return !getBodyFields().isEmpty(); } diff --git a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape.mustache b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape.mustache index 25bf01d350..c5955b768c 100644 --- a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape.mustache +++ b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape.mustache @@ -1,4 +1,15 @@ {{>Partials/ClassDeclaration}} { +{{#canBeSingleton}} + public {{className}}() {} + + /** + * Singleton instance for {@link {{className}}}. + */ + public static final {{className}} _INSTANCE = new {{className}}(); + + public static final {{TYPES.Client.Json.JsonpDeserializer}}<{{className}}> _DESERIALIZER = {{TYPES.Client.Json.JsonpDeserializer}}.fixedValue({{className}}._INSTANCE); +{{/canBeSingleton}} +{{^canBeSingleton}} {{>ObjectShape/Fields}} // --------------------------------------------------------------------------------------------- @@ -28,6 +39,7 @@ // --------------------------------------------------------------------------------------------- {{>ObjectShape/Deserialize}} +{{/canBeSingleton}} {{>ObjectShape/HashCode}} diff --git a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Deserialize.mustache b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Deserialize.mustache index 4d37bb6a2f..6d3e14591b 100644 --- a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Deserialize.mustache +++ b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Deserialize.mustache @@ -14,9 +14,9 @@ {{#extendsOtherShape}} setup{{extendsType.name}}Deserializer(op{{#extendsType.typeParams}}, {{>Type/deserializer}}{{/extendsType.typeParams}}); {{/extendsOtherShape}} - {{#bodyFields}} + {{#fieldsToDeserialize}} op.add({{#extendedByOtherShape}}Abstract{{/extendedByOtherShape}}Builder::{{name}}, {{#type}}{{>Type/deserializer}}{{/type}}, {{#quoted}}{{wireName}}{{/quoted}}{{#aliases}}, {{#quoted}}{{.}}{{/quoted}}{{/aliases}}); - {{/bodyFields}} + {{/fieldsToDeserialize}} {{#additionalPropertiesField}} op.setUnknownFieldHandler((builder, name, parser, mapper) -> { if (builder.{{name}} == null) { diff --git a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Serialize.mustache b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Serialize.mustache index 130c94bcec..36f3b13dcd 100644 --- a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Serialize.mustache +++ b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Serialize.mustache @@ -20,7 +20,7 @@ {{#additionalPropertiesField}} {{#type.directSerializer}}this.{{name}}{{/type.directSerializer}} {{/additionalPropertiesField}} -{{#bodyFields}} +{{#fieldsToSerialize}} {{^required}} if ({{#type.isDefined}}this.{{name}}{{/type.isDefined}}) { {{/required}} @@ -32,5 +32,5 @@ {{^-last}} {{/-last}} -{{/bodyFields}} +{{/fieldsToSerialize}} } \ No newline at end of file diff --git a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/RequestShape.mustache b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/RequestShape.mustache index f73e867c2f..4acc648e79 100644 --- a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/RequestShape.mustache +++ b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/RequestShape.mustache @@ -1,13 +1,13 @@ {{>Partials/ClassDeclaration}} { - {{#canBeSingleton}} +{{#canBeSingleton}} public {{className}}() {} /** * Singleton instance for {@link {{className}}}. */ public static final {{className}} _INSTANCE = new {{className}}(); - {{/canBeSingleton}} - {{^canBeSingleton}} +{{/canBeSingleton}} +{{^canBeSingleton}} {{>ObjectShape/Fields}} // --------------------------------------------------------------------------------------------- @@ -16,19 +16,21 @@ {{>ObjectShape/Getters}} - {{#hasRequestBody}} +{{#hasRequestBody}} {{>ObjectShape/Serialize}} - {{/hasRequestBody}} +{{/hasRequestBody}} // --------------------------------------------------------------------------------------------- {{>ObjectShape/Builder}} - {{#hasRequestBody}} +{{#hasRequestBody}} // --------------------------------------------------------------------------------------------- - {{>ObjectShape/Deserialize}}{{/hasRequestBody}} - {{/canBeSingleton}} + {{>ObjectShape/Deserialize}} + +{{/hasRequestBody}} +{{/canBeSingleton}} // --------------------------------------------------------------------------------------------- diff --git a/samples/src/main/java/org/opensearch/client/samples/DataStreamBasics.java b/samples/src/main/java/org/opensearch/client/samples/DataStreamBasics.java index 762e94c6bf..ac7a1cdd08 100644 --- a/samples/src/main/java/org/opensearch/client/samples/DataStreamBasics.java +++ b/samples/src/main/java/org/opensearch/client/samples/DataStreamBasics.java @@ -11,7 +11,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.client.opensearch.indices.CreateDataStreamRequest; -import org.opensearch.client.opensearch.indices.DataStream; import org.opensearch.client.opensearch.indices.DataStreamsStatsRequest; import org.opensearch.client.opensearch.indices.DataStreamsStatsResponse; import org.opensearch.client.opensearch.indices.DeleteDataStreamRequest; @@ -40,7 +39,7 @@ public static void main(String[] args) { // Create an index template which configures data stream PutIndexTemplateRequest putIndexTemplateRequest = new PutIndexTemplateRequest.Builder().name(dataStreamIndexTemplateName) .indexPatterns(namePattern) - .dataStream(new DataStream.Builder().timestampField(t -> t.name(timestampFieldName)).build()) + .dataStream(ds -> ds.timestampField(t -> t.name(timestampFieldName))) .build(); client.indices().putIndexTemplate(putIndexTemplateRequest);