Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sap.cloud.sdk.datamodel.openapi.generator;

import static com.sap.cloud.sdk.datamodel.openapi.generator.GeneratorCustomProperties.FIX_REDUNDANT_IS_BOOLEAN_PREFIX;
import static com.sap.cloud.sdk.datamodel.openapi.generator.GeneratorCustomProperties.USE_FLOAT_ARRAYS;
import static com.sap.cloud.sdk.datamodel.openapi.generator.GeneratorCustomProperties.USE_ONE_OF_CREATORS;

import java.nio.file.Path;
Expand All @@ -20,6 +21,7 @@
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.languages.JavaClientCodegen;
import org.openapitools.codegen.model.ModelMap;
Expand Down Expand Up @@ -84,6 +86,31 @@ private static JavaClientCodegen createCodegenConfig( @Nonnull final GenerationC
final var doubleIs = Pattern.compile("^isIs[A-Z]").asPredicate();
return new JavaClientCodegen()
{
@SuppressWarnings( { "rawtypes", "RedundantSuppression" } )
@Override
protected void updatePropertyForArray(
@Nonnull final CodegenProperty property,
@Nonnull final CodegenProperty innerProperty )
{
super.updatePropertyForArray(property, innerProperty);

if( USE_FLOAT_ARRAYS.isEnabled(config) && innerProperty.isNumber && property.isArray ) {
property.datatypeWithEnum = "float[]";
property.vendorExtensions.put("isPrimitiveArray", true);
}
}

@SuppressWarnings( { "rawtypes", "RedundantSuppression" } )
@Override
@Nullable
public String toDefaultValue( @Nonnull final CodegenProperty cp, @Nonnull final Schema schema )
{
if( USE_FLOAT_ARRAYS.isEnabled(config) && "float[]".equals(cp.datatypeWithEnum) ) {
return null;
}
return super.toDefaultValue(cp, schema);
}

@Override
@Nullable
public String toBooleanGetter( @Nullable final String name )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ enum GeneratorCustomProperties
/**
* Fix isIsBoolean() to isBoolean() for fields specified as `"isBoolean":{"type":"boolean"}`.
*/
FIX_REDUNDANT_IS_BOOLEAN_PREFIX("fixRedundantIsBooleanPrefix", "false");
FIX_REDUNDANT_IS_BOOLEAN_PREFIX("fixRedundantIsBooleanPrefix", "false"),

/**
* Use float arrays instead of big-decimal lists.
*/
USE_FLOAT_ARRAYS("useFloatArrays", "false");

private final String key;
private final String defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
return this;
}
{{#isArray}}
{{^vendorExtensions.isPrimitiveArray}}
/**
* Add one {{nameInCamelCase}} instance to this {@link {{classname}}}.
* @param {{name}}Item The {{nameInCamelCase}} that should be added
Expand All @@ -146,6 +147,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
return this;
{{/vendorExtensions.x-is-jackson-optional-nullable}}
}
{{/vendorExtensions.isPrimitiveArray}}
{{/isArray}}
{{#isMap}}

Expand Down Expand Up @@ -328,7 +330,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
}
final {{classname}} {{classVarName}} = ({{classname}}) o;
return Objects.equals(this.cloudSdkCustomFields, {{classVarName}}.cloudSdkCustomFields){{#hasVars}} &&
{{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}equalsNullable(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#isByteArray}}Arrays{{/isByteArray}}{{^isByteArray}}Objects{{/isByteArray}}.equals(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}} &&
{{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}equalsNullable(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#isByteArray}}Arrays{{/isByteArray}}{{^isByteArray}}{{^vendorExtensions.isPrimitiveArray}}Objects{{/vendorExtensions.isPrimitiveArray}}{{#vendorExtensions.isPrimitiveArray}}Arrays{{/vendorExtensions.isPrimitiveArray}}{{/isByteArray}}.equals(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}} &&
{{/-last}}{{/vars}}{{/hasVars}}{{#parent}} &&
super.equals(o){{/parent}};
{{/useReflectionEqualsHashCode}}
Expand All @@ -344,7 +346,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
return HashCodeBuilder.reflectionHashCode(this);
{{/useReflectionEqualsHashCode}}
{{^useReflectionEqualsHashCode}}
return Objects.hash({{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}hashCodeNullable({{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}}, {{/-last}}{{/vars}}{{#hasVars}}, {{/hasVars}}cloudSdkCustomFields{{#parent}}, super.hashCode(){{/parent}});
return Objects.hash({{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}hashCodeNullable({{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{^isByteArray}}{{^vendorExtensions.isPrimitiveArray}}{{name}}{{/vendorExtensions.isPrimitiveArray}}{{#vendorExtensions.isPrimitiveArray}}Arrays.hashCode({{name}}){{/vendorExtensions.isPrimitiveArray}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}}, {{/-last}}{{/vars}}{{#hasVars}}, {{/hasVars}}cloudSdkCustomFields{{#parent}}, super.hashCode(){{/parent}});
{{/useReflectionEqualsHashCode}}
}{{#vendorExtensions.x-jackson-optional-nullable-helpers}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private enum TestCase
true,
true,
6,
Map.of("aiSdkConstructor", "true", "fixRedundantIsBooleanPrefix", "true")),
Map.of("aiSdkConstructor", "true", "fixRedundantIsBooleanPrefix", "true", "useFloatArrays", "true")),
API_CLASS_VENDOR_EXTENSION_YAML(
"api-class-vendor-extension-yaml",
"sodastore.yaml",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,15 @@
"price": {
"type": "number",
"format": "float"
},
"embedding":{
"type": "array",
"items": {
"type": "number"
}
}
},
"required": ["name", "brand", "flavor", "price"]
"required": ["name", "brand", "flavor", "price", "embedding"]
},
"NewSoda": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonValue;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down Expand Up @@ -59,6 +63,9 @@ public class Soda
@JsonProperty("price")
private Float price;

@JsonProperty("embedding")
private float[] embedding;
Comment on lines +66 to +67
Copy link
Contributor Author

@newtork newtork Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For comparison, without feature toggle:

@JsonProperty("embedding")
private List<BigDecimal> embedding = new ArrayList<>();


@JsonAnySetter
@JsonAnyGetter
private final Map<String, Object> cloudSdkCustomFields = new LinkedHashMap<>();
Expand Down Expand Up @@ -237,6 +244,35 @@ public void setPrice( @Nonnull final Float price) {
this.price = price;
}

/**
* Set the embedding of this {@link Soda} instance and return the same instance.
*
* @param embedding The embedding of this {@link Soda}
* @return The same instance of this {@link Soda} class
*/
@Nonnull public Soda embedding( @Nonnull final float[] embedding) {
this.embedding = embedding;
return this;
}

/**
* Get embedding
* @return embedding The embedding of this {@link Soda} instance.
*/
@Nonnull
public float[] getEmbedding() {
return embedding;
}

/**
* Set the embedding of this {@link Soda} instance.
*
* @param embedding The embedding of this {@link Soda}
*/
public void setEmbedding( @Nonnull final float[] embedding) {
this.embedding = embedding;
}

/**
* Get the names of the unrecognizable properties of the {@link Soda}.
* @return The set of properties names
Expand Down Expand Up @@ -279,6 +315,7 @@ public Map<String, Object> toMap()
if( isAvailable != null ) declaredFields.put("isAvailable", isAvailable);
if( flavor != null ) declaredFields.put("flavor", flavor);
if( price != null ) declaredFields.put("price", price);
if( embedding != null ) declaredFields.put("embedding", embedding);
return declaredFields;
}

Expand Down Expand Up @@ -310,12 +347,13 @@ public boolean equals(@Nullable final java.lang.Object o) {
Objects.equals(this.brand, soda.brand) &&
Objects.equals(this.isAvailable, soda.isAvailable) &&
Objects.equals(this.flavor, soda.flavor) &&
Objects.equals(this.price, soda.price);
Objects.equals(this.price, soda.price) &&
Arrays.equals(this.embedding, soda.embedding);
}

@Override
public int hashCode() {
return Objects.hash(id, name, brand, isAvailable, flavor, price, cloudSdkCustomFields);
return Objects.hash(id, name, brand, isAvailable, flavor, price, Arrays.hashCode(embedding), cloudSdkCustomFields);
}

@Override
Expand All @@ -328,6 +366,7 @@ public int hashCode() {
sb.append(" isAvailable: ").append(toIndentedString(isAvailable)).append("\n");
sb.append(" flavor: ").append(toIndentedString(flavor)).append("\n");
sb.append(" price: ").append(toIndentedString(price)).append("\n");
sb.append(" embedding: ").append(toIndentedString(embedding)).append("\n");
cloudSdkCustomFields.forEach((k,v) -> sb.append(" ").append(k).append(": ").append(toIndentedString(v)).append("\n"));
sb.append("}");
return sb.toString();
Expand Down
Loading