Skip to content

Commit

Permalink
Handle deprecation
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Jun 6, 2024
1 parent 4b39205 commit 8caaf10
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,4 +60,9 @@ public void setRequired(boolean required) {
public String getDescription() {
return description;
}

@Nullable
public Deprecation getDeprecation() {
return deprecation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down Expand Up @@ -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))
)
);

Expand All @@ -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
)
);
}
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -366,6 +373,14 @@ private Type mapOneOf(List<OpenApiSchema> oneOf) {
throw new UnsupportedOperationException("Can not get type name for oneOf: " + oneOf);
}

private Type mapAllOf(List<OpenApiSchema> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenApiParameter> {
@Nullable
Expand All @@ -26,7 +28,14 @@ public class OpenApiParameter extends OpenApiRefElement<OpenApiParameter> {
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);
Expand All @@ -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
Expand Down Expand Up @@ -64,6 +77,16 @@ public Optional<OpenApiSchema> getSchema() {
}

public boolean isGlobal() {
return isGlobal;
return isGlobal != null && isGlobal;
}

public boolean isDeprecated() {
return isDeprecated != null && isDeprecated;
}

@Nonnull
public Optional<Deprecation> getDeprecation() {
if (versionDeprecated == null && deprecationMessage == null) return Optional.empty();
return Optional.of(new Deprecation(deprecationMessage, versionDeprecated));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <TKey, TValue> Optional<TValue> tryGet(@Nullable Map<TKey, TValue> 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 <TKey, TValue> Map<TKey, TValue> createLookupOf(@Nonnull TValue[] values, @Nonnull Function<TValue, TKey> by) {
Objects.requireNonNull(values, "values must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,53 @@
{{#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;
}
{{/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;
}
{{#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());
}
{{/type.listValueType.hasBuilder}}
{{/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;
}
{{#type.hasBuilder}}

{{>ObjectShape/FieldDoc/Basic}}
{{#deprecation}}@Deprecated{{/deprecation}}
public final Builder {{name}}({{type.builderFnType}} fn) {
return this.{{name}}(fn.apply(new {{type.builderType}}()).build());
}
Expand All @@ -67,4 +74,4 @@
return new {{className}}(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{{#fields}}

{{#deprecation}}@Deprecated{{/deprecation}}
{{^required}}
{{^type.isListOrMap}}
@{{TYPES.Javax.Annotation.Nullable}}
{{/type.isListOrMap}}
{{/required}}
private final {{type}} {{name}};
{{/fields}}
{{/fields}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{{#fields}}

{{>ObjectShape/FieldDoc/Basic}}
{{#deprecation}}@Deprecated{{/deprecation}}
{{^required}}
{{^type.isListOrMap}}
@{{TYPES.Javax.Annotation.Nullable}}
Expand All @@ -9,4 +10,4 @@
public final {{type}} {{name}}() {
return this.{{name}};
}
{{/fields}}
{{/fields}}

0 comments on commit 8caaf10

Please sign in to comment.