Skip to content

Commit

Permalink
Handle default Property variant
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Nov 13, 2024
1 parent d5d38fc commit a3023df
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ protected static void setupPropertyDeserializer(ObjectDeserializer<Builder> 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<Property> _DESERIALIZER = ObjectBuilderDeserializer.lazy(
Expand Down
8 changes: 5 additions & 3 deletions java-codegen/opensearch-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@
public class TaggedUnionShape extends Shape {
private final Map<String, Variant> variants = new TreeMap<>();
private final String discriminatingField;
private final String defaultVariant;

public TaggedUnionShape(
Namespace parent,
String className,
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) {
Expand All @@ -49,7 +52,7 @@ public Collection<Variant> getNonShapeVariants() {
}

public Variant getDefaultVariant() {
return variants.get("custom");
return variants.get(this.defaultVariant != null ? this.defaultVariant : "custom");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,32 @@
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;

public class OpenApiDiscriminator extends OpenApiElement<OpenApiDiscriminator> {
@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<String> getPropertyName() {
return Optional.ofNullable(propertyName);
}

@Nonnull
public Optional<String> getDefaultValue() {
return Optional.ofNullable(defaultValue);
}
}

0 comments on commit a3023df

Please sign in to comment.