From a3023df29c3e72f8ab474d7ec5f33c4883dc21a1 Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Thu, 14 Nov 2024 00:17:18 +1300 Subject: [PATCH] Handle default Property variant Signed-off-by: Thomas Farr --- .../client/opensearch/_types/mapping/Property.java | 2 +- java-codegen/opensearch-openapi.yaml | 8 +++++--- .../client/codegen/model/SpecTransformer.java | 3 ++- .../client/codegen/model/TaggedUnionShape.java | 5 ++++- .../client/codegen/openapi/OpenApiDiscriminator.java | 11 +++++++++++ 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/mapping/Property.java b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/mapping/Property.java index 853df195cf..77fa8fa51c 100644 --- a/java-client/src/generated/java/org/opensearch/client/opensearch/_types/mapping/Property.java +++ b/java-client/src/generated/java/org/opensearch/client/opensearch/_types/mapping/Property.java @@ -1420,7 +1420,7 @@ protected static void setupPropertyDeserializer(ObjectDeserializer op) op.add(Builder::xyPoint, XyPointProperty._DESERIALIZER, "xy_point"); op.add(Builder::xyShape, XyShapeProperty._DESERIALIZER, "xy_shape"); - op.setTypeProperty("type", null); + op.setTypeProperty("type", Kind.Object.jsonValue()); } public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy( diff --git a/java-codegen/opensearch-openapi.yaml b/java-codegen/opensearch-openapi.yaml index e37a4aa1f3..8dd8aab304 100644 --- a/java-codegen/opensearch-openapi.yaml +++ b/java-codegen/opensearch-openapi.yaml @@ -26130,7 +26130,7 @@ components: The index template with the highest priority is chosen. If no priority is specified the template is treated as though it is of priority 0 (lowest priority). This number is not automatically generated by OpenSearch. - type: number + type: integer version: $ref: '#/components/schemas/_common___VersionNumber' _meta: @@ -26310,7 +26310,7 @@ components: The index template with the highest priority is chosen. If no priority is specified the template is treated as though it is of priority 0 (lowest priority). This number is not automatically generated by OpenSearch. - type: number + type: integer version: $ref: '#/components/schemas/_common___VersionNumber' _meta: @@ -39556,6 +39556,7 @@ components: type: object discriminator: propertyName: type + x-default: object oneOf: - $ref: '#/components/schemas/_common.mapping___BinaryProperty' - $ref: '#/components/schemas/_common.mapping___BooleanProperty' @@ -49100,7 +49101,8 @@ components: The index template with the highest priority is chosen. If no priority is specified the template is treated as though it is of priority 0 (lowest priority). This number is not automatically generated by OpenSearch. - type: number + type: integer + format: int64 _meta: $ref: '#/components/schemas/_common___Metadata' allow_auto_create: 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 7860692b56..61d9a6b594 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 @@ -284,7 +284,8 @@ private Shape visit(Namespace parent, String className, String typedefName, Open visitInto(schema, enumShape); } else if (isTaggedUnion) { var discriminatingField = schema.getDiscriminator().flatMap(OpenApiDiscriminator::getPropertyName).orElse(null); - var taggedUnion = new TaggedUnionShape(parent, className, typedefName, description, discriminatingField, shouldGenerate); + var defaultVariant = schema.getDiscriminator().flatMap(OpenApiDiscriminator::getDefaultValue).orElse(null); + var taggedUnion = new TaggedUnionShape(parent, className, typedefName, description, discriminatingField, defaultVariant, shouldGenerate); shape = taggedUnion; visitedSchemas.putIfAbsent(schema, shape); diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/model/TaggedUnionShape.java b/java-codegen/src/main/java/org/opensearch/client/codegen/model/TaggedUnionShape.java index 7214c6c374..681a19b059 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/model/TaggedUnionShape.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/model/TaggedUnionShape.java @@ -22,6 +22,7 @@ public class TaggedUnionShape extends Shape { private final Map variants = new TreeMap<>(); private final String discriminatingField; + private final String defaultVariant; public TaggedUnionShape( Namespace parent, @@ -29,10 +30,12 @@ public TaggedUnionShape( String typedefName, String description, String discriminatingField, + String defaultVariant, ShouldGenerate shouldGenerate ) { super(parent, className, typedefName, description, shouldGenerate); this.discriminatingField = discriminatingField; + this.defaultVariant = defaultVariant; } public void addVariant(String name, Type type) { @@ -49,7 +52,7 @@ public Collection getNonShapeVariants() { } public Variant getDefaultVariant() { - return variants.get("custom"); + return variants.get(this.defaultVariant != null ? this.defaultVariant : "custom"); } @Override diff --git a/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/OpenApiDiscriminator.java b/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/OpenApiDiscriminator.java index 2cf855dbb4..77860701b8 100644 --- a/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/OpenApiDiscriminator.java +++ b/java-codegen/src/main/java/org/opensearch/client/codegen/openapi/OpenApiDiscriminator.java @@ -9,6 +9,8 @@ package org.opensearch.client.codegen.openapi; import io.swagger.v3.oas.models.media.Discriminator; +import org.opensearch.client.codegen.utils.Maps; + import java.util.Optional; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -16,14 +18,23 @@ public class OpenApiDiscriminator extends OpenApiElement { @Nullable private final String propertyName; + @Nullable + private final String defaultValue; protected OpenApiDiscriminator(@Nullable OpenApiElement parent, @Nonnull JsonPointer pointer, @Nonnull Discriminator discriminator) { super(parent, pointer); this.propertyName = discriminator.getPropertyName(); + var extensions = discriminator.getExtensions(); + this.defaultValue = (String) Maps.tryGet(extensions, "x-default").orElse(null); } @Nonnull public Optional getPropertyName() { return Optional.ofNullable(propertyName); } + + @Nonnull + public Optional getDefaultValue() { + return Optional.ofNullable(defaultValue); + } }