From 8caaf100a36aa8683ee29ba58501fa1ee242ef84 Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Fri, 7 Jun 2024 10:35:43 +1200 Subject: [PATCH] Handle deprecation Signed-off-by: Thomas Farr --- .../client/codegen/model/ArrayShape.java | 2 +- .../client/codegen/model/Field.java | 10 ++++++- .../client/codegen/model/SpecTransformer.java | 21 ++++++++++++-- .../codegen/openapi/OpenApiParameter.java | 29 +++++++++++++++++-- .../opensearch/client/codegen/utils/Maps.java | 7 ++++- .../templates/ObjectShape/Builder.mustache | 9 +++++- .../templates/ObjectShape/Fields.mustache | 3 +- .../templates/ObjectShape/Getters.mustache | 3 +- 8 files changed, 72 insertions(+), 12 deletions(-) diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/model/ArrayShape.java b/java-codegen/src/main/java/org/opensearch/client/codegen/model/ArrayShape.java index a400124939..c45e73fda9 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/model/ArrayShape.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/model/ArrayShape.java @@ -16,7 +16,7 @@ public class ArrayShape extends ObjectShape { public ArrayShape(Namespace parent, String className, Type arrayType, String typedefName) { super(parent, className, typedefName); - this.valueBodyField = new Field("_value_body", arrayType, true, "Response value."); + this.valueBodyField = new Field("_value_body", arrayType, true, "Response value.", null); } @Override diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/model/Field.java b/java-codegen/src/main/java/org/opensearch/client/codegen/model/Field.java index 84b3c1063d..5beee6b0d3 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/model/Field.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/model/Field.java @@ -22,12 +22,15 @@ public class Field { private boolean required; @Nullable private final String description; + @Nullable + private final Deprecation deprecation; - public Field(@Nonnull String wireName, @Nonnull Type type, boolean required, @Nullable String description) { + public Field(@Nonnull String wireName, @Nonnull Type type, boolean required, @Nullable String description, @Nullable Deprecation deprecation) { this.wireName = Strings.requireNonBlank(wireName, "wireName must not be null"); this.type = Objects.requireNonNull(type, "type must not be null"); this.required = required; this.description = description; + this.deprecation = deprecation; } @Nonnull @@ -57,4 +60,9 @@ public void setRequired(boolean required) { public String getDescription() { return description; } + + @Nullable + public Deprecation getDeprecation() { + return deprecation; + } } diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/model/SpecTransformer.java b/java-codegen/src/main/java/org/opensearch/client/codegen/model/SpecTransformer.java index f896fc1dc1..7f2e0896c9 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/model/SpecTransformer.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/model/SpecTransformer.java @@ -209,7 +209,8 @@ private Field visit(OpenApiParameter parameter) { parameter.getName().orElseThrow(), mapType(parameter.getSchema().orElseThrow()), parameter.getRequired(), - parameter.getDescription().orElse(null) + parameter.getDescription().orElse(null), + parameter.getDeprecation().orElse(null) ); } @@ -267,7 +268,7 @@ private void visitInto(OpenApiSchema schema, ObjectShape shape) { schema.getProperties() .ifPresent( props -> props.forEach( - (k, v) -> shape.addBodyField(new Field(k, mapType(v), required.contains(k), v.getDescription().orElse(null))) + (k, v) -> shape.addBodyField(new Field(k, mapType(v), required.contains(k), v.getDescription().orElse(null), null)) ) ); @@ -279,7 +280,8 @@ private void visitInto(OpenApiSchema schema, ObjectShape shape) { "metadata", Types.Java.Util.Map(Types.Java.Lang.String, valueType), false, - additionalProperties.get().getDescription().orElse(null) + additionalProperties.get().getDescription().orElse(null), + null ) ); } @@ -320,6 +322,11 @@ private Type mapTypeInner(OpenApiSchema schema) { return mapOneOf(oneOf.get()); } + var allOf = schema.getAllOf(); + if (allOf.isPresent()) { + return mapAllOf(allOf.get()); + } + var type = schema.getType(); if (type.isEmpty()) { @@ -366,6 +373,14 @@ private Type mapOneOf(List oneOf) { throw new UnsupportedOperationException("Can not get type name for oneOf: " + oneOf); } + private Type mapAllOf(List allOf) { + if (allOf.size() == 1) { + return mapType(allOf.get(0)); + } + + throw new UnsupportedOperationException("Can not get type name for allOf: " + allOf); + } + private Type mapObject(OpenApiSchema schema) { var values = schema.getAdditionalProperties().map(s -> mapType(s, true)).orElse(Types.Client.Json.JsonData); return Types.Java.Util.Map(Types.Java.Lang.String, values); diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/OpenApiParameter.java b/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/OpenApiParameter.java index 1f476896d6..e2a514509f 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/OpenApiParameter.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/OpenApiParameter.java @@ -14,6 +14,8 @@ import java.util.Optional; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.opensearch.client.codegen.model.Deprecation; +import org.opensearch.client.codegen.utils.Maps; public class OpenApiParameter extends OpenApiRefElement { @Nullable @@ -26,7 +28,14 @@ public class OpenApiParameter extends OpenApiRefElement { private final Boolean isRequired; @Nullable private final OpenApiSchema schema; - private final boolean isGlobal; + @Nullable + private final Boolean isDeprecated; + @Nullable + private final String versionDeprecated; + @Nullable + private final String deprecationMessage; + @Nullable + private final Boolean isGlobal; protected OpenApiParameter(@Nullable OpenApiElement parent, @Nonnull JsonPointer pointer, @Nonnull Parameter parameter) { super(parent, pointer, parameter.get$ref(), OpenApiParameter.class); @@ -35,8 +44,12 @@ protected OpenApiParameter(@Nullable OpenApiElement parent, @Nonnull JsonPoin this.in = ifNonnull(parameter.getIn(), In::from); this.isRequired = parameter.getRequired(); this.schema = child("schema", parameter.getSchema(), OpenApiSchema::new); + this.isDeprecated = parameter.getDeprecated(); var extensions = parameter.getExtensions(); - this.isGlobal = extensions != null && Boolean.TRUE.equals(extensions.get("x-global")); + this.versionDeprecated = Maps.tryGet(extensions, "x-version-deprecated").map(String::valueOf).orElse(null); + this.deprecationMessage = Maps.tryGet(extensions, "x-deprecation-message").map(String::valueOf).orElse(null); + this.isGlobal = (Boolean) Maps.tryGet(extensions, "x-global") + .orElse(null); } @Nonnull @@ -64,6 +77,16 @@ public Optional getSchema() { } public boolean isGlobal() { - return isGlobal; + return isGlobal != null && isGlobal; + } + + public boolean isDeprecated() { + return isDeprecated != null && isDeprecated; + } + + @Nonnull + public Optional getDeprecation() { + if (versionDeprecated == null && deprecationMessage == null) return Optional.empty(); + return Optional.of(new Deprecation(deprecationMessage, versionDeprecated)); } } diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/utils/Maps.java b/java-codegen/src/main/java/org/opensearch/client/codegen/utils/Maps.java index 3d8dea590f..b16f6f039f 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/utils/Maps.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/utils/Maps.java @@ -16,13 +16,18 @@ import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; -import java.util.stream.StreamSupport; import javax.annotation.Nonnull; import javax.annotation.Nullable; public final class Maps { private Maps() {} + @Nonnull + public static Optional tryGet(@Nullable Map map, @Nonnull TKey key) { + Objects.requireNonNull(key, "key must not be null"); + return Optional.ofNullable(map).flatMap(m -> Optional.ofNullable(m.get(key))); + } + @Nonnull public static Map createLookupOf(@Nonnull TValue[] values, @Nonnull Function by) { Objects.requireNonNull(values, "values must not be null"); diff --git a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Builder.mustache b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Builder.mustache index 3c7d09a7a2..56d73c16c1 100644 --- a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Builder.mustache +++ b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Builder.mustache @@ -9,12 +9,14 @@ {{#fields}} {{#type.isMap}} {{>ObjectShape/FieldDoc/Basic}} + {{#deprecation}}@Deprecated{{/deprecation}} public final Builder {{name}}({{type}} map) { this.{{name}} = _mapPutAll(this.{{name}}, map); return this; } {{>ObjectShape/FieldDoc/Basic}} + {{#deprecation}}@Deprecated{{/deprecation}} public final Builder {{name}}({{type.mapKeyType}} key, {{type.mapValueType}} value) { this.{{name}} = _mapPut(this.{{name}}, key, value); return this; @@ -22,12 +24,14 @@ {{/type.isMap}} {{#type.isList}} {{>ObjectShape/FieldDoc/ListAddAll}} + {{#deprecation}}@Deprecated{{/deprecation}} public final Builder {{name}}({{type}} list) { this.{{name}} = _listAddAll(this.{{name}}, list); return this; } {{>ObjectShape/FieldDoc/ListAdd}} + {{#deprecation}}@Deprecated{{/deprecation}} public final Builder {{name}}({{type.listValueType}} value, {{type.listValueType}}... values) { this.{{name}} = _listAdd(this.{{name}}, value, values); return this; @@ -35,6 +39,7 @@ {{#type.listValueType.hasBuilder}} {{>ObjectShape/FieldDoc/ListAddBuilderFn}} + {{#deprecation}}@Deprecated{{/deprecation}} public final Builder {{name}}({{type.listValueType.builderFnType}} fn) { return {{name}}(fn.apply(new {{type.listValueType.builderType}}()).build()); } @@ -42,6 +47,7 @@ {{/type.isList}} {{^type.isListOrMap}} {{>ObjectShape/FieldDoc/Basic}} + {{#deprecation}}@Deprecated{{/deprecation}} public final Builder {{name}}({{^required}}@{{TYPES.Javax.Annotation.Nullable}} {{/required}}{{type}} value) { this.{{name}} = value; return this; @@ -49,6 +55,7 @@ {{#type.hasBuilder}} {{>ObjectShape/FieldDoc/Basic}} + {{#deprecation}}@Deprecated{{/deprecation}} public final Builder {{name}}({{type.builderFnType}} fn) { return this.{{name}}(fn.apply(new {{type.builderType}}()).build()); } @@ -67,4 +74,4 @@ return new {{className}}(this); } - } \ No newline at end of file + } diff --git a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Fields.mustache b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Fields.mustache index 3f228dd1f5..8c40ad79d8 100644 --- a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Fields.mustache +++ b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Fields.mustache @@ -1,9 +1,10 @@ {{#fields}} + {{#deprecation}}@Deprecated{{/deprecation}} {{^required}} {{^type.isListOrMap}} @{{TYPES.Javax.Annotation.Nullable}} {{/type.isListOrMap}} {{/required}} private final {{type}} {{name}}; -{{/fields}} \ No newline at end of file +{{/fields}} diff --git a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Getters.mustache b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Getters.mustache index 053da22406..61fea36e57 100644 --- a/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Getters.mustache +++ b/java-codegen/src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Getters.mustache @@ -1,6 +1,7 @@ {{#fields}} {{>ObjectShape/FieldDoc/Basic}} + {{#deprecation}}@Deprecated{{/deprecation}} {{^required}} {{^type.isListOrMap}} @{{TYPES.Javax.Annotation.Nullable}} @@ -9,4 +10,4 @@ public final {{type}} {{name}}() { return this.{{name}}; } -{{/fields}} \ No newline at end of file +{{/fields}}