Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.
Merged
1 change: 1 addition & 0 deletions bin/generate_samples_configs/java_3_0_3_unit_test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
generatorName: java
outputDir: samples/client/3_0_3_unit_test/java
inputSpec: src/test/resources/3_0/unit_test_spec/3_0_3_unit_test_spec_nopaths.yaml
intsAllowedForFloatDoubleFormats: true
additionalProperties:
artifactId: unit-test-api
hideGenerationTimestamp: "true"
1 change: 1 addition & 0 deletions bin/generate_samples_configs/java_3_1_0_unit_test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
generatorName: java
outputDir: samples/client/3_1_0_unit_test/java
inputSpec: src/test/resources/3_1/unit_test_spec/3_1_0_unit_test_spec_nopaths.yaml
intsAllowedForFloatDoubleFormats: true
additionalProperties:
artifactId: unit-test-api
1 change: 1 addition & 0 deletions bin/generate_samples_configs/python_3_0_3_unit_test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
generatorName: python
outputDir: samples/client/3_0_3_unit_test/python
inputSpec: src/test/resources/3_0/unit_test_spec/3_0_3_unit_test_spec.yaml
intsAllowedForFloatDoubleFormats: true
additionalProperties:
packageName: unit_test_api
1 change: 1 addition & 0 deletions bin/generate_samples_configs/python_3_1_0_unit_test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
generatorName: python
outputDir: samples/client/3_1_0_unit_test/python
inputSpec: src/test/resources/3_1/unit_test_spec/3_1_0_unit_test_spec.yaml
intsAllowedForFloatDoubleFormats: true
additionalProperties:
packageName: unit_test_api
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ public class Generate extends AbstractCommand {
description = CodegenConstants.SKIP_OPERATION_EXAMPLE_DESC)
private Boolean skipOperationExample;

@Option(name = {"--ints-allowed-for-float-double-formats"}, title = "allow int payloads when type is number and format is double/float",
description = CodegenConstants.INTS_ALLOWED_FOR_FLOAT_DOUBLE_FORMATS_DESC)
private Boolean intsAllowedForFloatDoubleFormats;

@Option(name = {"--skip-validate-spec"},
title = "skip spec validation",
description = "Skips the default behavior of validating an input specification.")
Expand Down Expand Up @@ -327,6 +331,10 @@ public void execute() {
configurator.setRemoveEnumValuePrefix(removeEnumValuePrefix);
}

if (intsAllowedForFloatDoubleFormats != null) {
configurator.setIntsAllowedForFloatDoubleFormats(intsAllowedForFloatDoubleFormats);
}

if (hideGenerationTimestamp != null) {
configurator.setHideGenerationTimestamp(hideGenerationTimestamp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
public static final String INIT_REQUIRED_VARS = "initRequiredVars";
public static final String INIT_REQUIRED_VARS_DESC = "If set to true then the required variables are included as positional arguments in __init__ and _from_openapi_data methods. Note: this can break some composition use cases. To learn more read PR #8802.";

public static final String INTS_ALLOWED_FOR_FLOAT_DOUBLE_FORMATS = "intsAllowedForFloatDoubleFormats";

public static final String INTS_ALLOWED_FOR_FLOAT_DOUBLE_FORMATS_DESC = "Integers are allowed in for type: number format:float/double payloads";

public static final String NON_COMPLIANT_USE_DISCR_IF_COMPOSITION_FAILS = "nonCompliantUseDiscriminatorIfCompositionFails";

public static final String NON_COMPLIANT_USE_DISCR_IF_COMPOSITION_FAILS_DESC =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ public CodegenConfigurator setRemoveEnumValuePrefix(boolean removeEnumValuePrefi
return this;
}

public CodegenConfigurator setIntsAllowedForFloatDoubleFormats(boolean intsAllowedForFloatDoubleFormats) {
workflowSettingsBuilder.withIntsAllowedForFloatDoubleFormats(intsAllowedForFloatDoubleFormats);
return this;
}

public CodegenConfigurator setHideGenerationTimestamp(boolean hideGenerationTimestamp) {
workflowSettingsBuilder.withHideGenerationTimestamp(hideGenerationTimestamp);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class WorkflowSettings {
public static final String DEFAULT_TEMPLATING_ENGINE_NAME = "handlebars";

public static final boolean DEFAULT_HIDE_GENERATION_TIMESTAMP = true;
public static final boolean DEFAULT_INTS_ALLOWED_FOR_FLOAT_DOUBLE_FORMATS = false; // TODO in version 5.0.0 release change this to true
public static final Map<String, String> DEFAULT_GLOBAL_PROPERTIES = Collections.unmodifiableMap(new HashMap<>());

private String inputSpec;
Expand All @@ -68,6 +69,7 @@ public class WorkflowSettings {
private Map<String, ?> globalProperties = DEFAULT_GLOBAL_PROPERTIES;
private boolean removeEnumValuePrefix = DEFAULT_REMOVE_ENUM_VALUE_PREFIX;
private Boolean hideGenerationTimestamp = DEFAULT_HIDE_GENERATION_TIMESTAMP;
private Boolean intsAllowedForFloatDoubleFormats = DEFAULT_INTS_ALLOWED_FOR_FLOAT_DOUBLE_FORMATS;

private WorkflowSettings(Builder builder) {
this.inputSpec = builder.inputSpec;
Expand All @@ -87,6 +89,7 @@ private WorkflowSettings(Builder builder) {
this.globalProperties = Collections.unmodifiableMap(builder.globalProperties);
this.removeEnumValuePrefix = builder.removeEnumValuePrefix;
this.hideGenerationTimestamp = builder.hideGenerationTimestamp;
this.intsAllowedForFloatDoubleFormats = builder.intsAllowedForFloatDoubleFormats;
}

/**
Expand Down Expand Up @@ -121,6 +124,7 @@ public static Builder newBuilder(WorkflowSettings copy) {
builder.globalProperties = new HashMap<>(copy.getGlobalProperties());
builder.removeEnumValuePrefix = copy.isRemoveEnumValuePrefix();
builder.hideGenerationTimestamp = copy.isHideGenerationTimestamp();
builder.intsAllowedForFloatDoubleFormats = copy.isIntsAllowedForFloatDoubleFormats();

// force builder "with" methods to invoke side effects
builder.withTemplateDir(copy.getTemplateDir());
Expand Down Expand Up @@ -180,6 +184,9 @@ public boolean isRemoveEnumValuePrefix() {
}

public boolean isHideGenerationTimestamp() { return hideGenerationTimestamp; }

public boolean isIntsAllowedForFloatDoubleFormats() { return intsAllowedForFloatDoubleFormats; }

/**
* Indicates whether or not to skip examples defined in the operation.
*
Expand Down Expand Up @@ -314,14 +321,21 @@ public static final class Builder {
private String templateDir;
private String templatingEngineName = DEFAULT_TEMPLATING_ENGINE_NAME;
private String ignoreFileOverride;
private boolean hideGenerationTimestamp = DEFAULT_HIDE_GENERATION_TIMESTAMP;
private Boolean hideGenerationTimestamp = DEFAULT_HIDE_GENERATION_TIMESTAMP;
private Boolean intsAllowedForFloatDoubleFormats = DEFAULT_INTS_ALLOWED_FOR_FLOAT_DOUBLE_FORMATS;

// NOTE: All collections must be mutable in the builder, and copied to a new immutable collection in .build()
private Map<String, String> globalProperties = new HashMap<>();

private Builder() {
}

public Builder withIntsAllowedForFloatDoubleFormats(Boolean intsAllowedForFloatDoubleFormats) {
if (intsAllowedForFloatDoubleFormats != null) {
this.intsAllowedForFloatDoubleFormats = intsAllowedForFloatDoubleFormats;
}
return this;
}

/**
* Sets the {@code inputSpec} and returns a reference to this Builder so that the methods can be chained together.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ private void generateFile(Map<String, Object> templateData, String templateName,
templateData.putAll(generator.additionalProperties());
String packageName = generator.generatorSettings().packageName;
templateData.put("packageName", packageName);
templateData.put("generatorSettings", generator.generatorSettings());
try {
File written = processTemplateToFile(templateData, templateName, outputFilename, shouldGenerate, skippedByOption);
if (written != null) {
Expand Down Expand Up @@ -877,6 +878,7 @@ private void generateXDocs(List<File> files, String jsonPath, CodegenConstants.J
HashMap<String, Object> templateData = new HashMap<>();
templateData.putAll(generator.additionalProperties());
templateData.put("packageName", generator.generatorSettings().packageName);
templateData.put("generatorSettings", generator.generatorSettings());
templateData.put("modelPackage", generator.modelPackage());
if (templateInfo != null && !templateInfo.isEmpty()) {
templateData.putAll(templateInfo);
Expand Down Expand Up @@ -908,6 +910,7 @@ private void generateXTests(List<File> files, String jsonPath, CodegenConstants.
HashMap<String, Object> templateData = new HashMap<>();
templateData.putAll(generator.additionalProperties());
templateData.put("packageName", generator.generatorSettings().packageName);
templateData.put("generatorSettings", generator.generatorSettings());
templateData.put("modelPackage", generator.modelPackage());
if (templateInfo != null && !templateInfo.isEmpty()) {
templateData.putAll(templateInfo);
Expand Down Expand Up @@ -944,6 +947,7 @@ private void generateXs(List<File> files, String jsonPath, CodegenConstants.JSON
HashMap<String, Object> templateData = new HashMap<>();
templateData.putAll(generator.additionalProperties());
templateData.put("packageName", generator.generatorSettings().packageName);
templateData.put("generatorSettings", generator.generatorSettings());
templateData.put("modelPackage", generator.modelPackage());
if (templateInfo != null && !templateInfo.isEmpty()) {
templateData.putAll(templateInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class CodeGeneratorSettings {
public final String inputSpecLocation; // input spec's location, as URL or file
public final boolean removeEnumValuePrefix;
public final boolean hideGenerationTimestamp;
public final boolean intsAllowedForFloatDoubleFormats;
public CodeGeneratorSettings(
String apiPackage,
String artifactId,
Expand All @@ -40,7 +41,8 @@ public CodeGeneratorSettings(
String templateEngineName,
String inputSpecLocation,
boolean removeEnumValuePrefix,
boolean hideGenerationTimestamp
boolean hideGenerationTimestamp,
boolean intsAllowedForFloatDoubleFormats
) {
this.apiPackage = apiPackage;
this.artifactId = artifactId;
Expand All @@ -59,6 +61,7 @@ public CodeGeneratorSettings(
this.inputSpecLocation = inputSpecLocation;
this.removeEnumValuePrefix = removeEnumValuePrefix;
this.hideGenerationTimestamp = hideGenerationTimestamp;
this.intsAllowedForFloatDoubleFormats = intsAllowedForFloatDoubleFormats;
}

public static CodeGeneratorSettings of(GeneratorSettings generatorSettings, WorkflowSettings workflowSettings, String embeddedTemplateDir, String packageNameDefault, String artifactIdDefault, String outputFolderDefault) {
Expand All @@ -79,6 +82,7 @@ public static CodeGeneratorSettings of(GeneratorSettings generatorSettings, Work
String inputSpecLocation = workflowSettings != null ? workflowSettings.getInputSpec() : null;
boolean removeEnumValuePrefix = workflowSettings != null ? workflowSettings.isRemoveEnumValuePrefix() : WorkflowSettings.DEFAULT_REMOVE_ENUM_VALUE_PREFIX;
boolean hideGenerationTimestamp = workflowSettings != null ? workflowSettings.isHideGenerationTimestamp() : WorkflowSettings.DEFAULT_HIDE_GENERATION_TIMESTAMP;
boolean intsAllowedForFloatDoubleFormats = workflowSettings != null ? workflowSettings.isIntsAllowedForFloatDoubleFormats() : WorkflowSettings.DEFAULT_INTS_ALLOWED_FOR_FLOAT_DOUBLE_FORMATS;
return new CodeGeneratorSettings(
apiPackage,
artifactId,
Expand All @@ -96,7 +100,8 @@ public static CodeGeneratorSettings of(GeneratorSettings generatorSettings, Work
templateEnginName,
inputSpecLocation,
removeEnumValuePrefix,
hideGenerationTimestamp
hideGenerationTimestamp,
intsAllowedForFloatDoubleFormats
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ public class DoubleJsonSchema {

protected DoubleJsonSchema1() {
super(new JsonSchemaInfo()
{{#if generatorSettings.intsAllowedForFloatDoubleFormats}}
.type(Set.of(
Integer.class,
Long.class,
Float.class,
Double.class
))
{{else}}
.type(Set.of(Double.class))
{{/if}}
.format("double")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ public class FloatJsonSchema {

protected FloatJsonSchema1() {
super(new JsonSchemaInfo()
{{#if generatorSettings.intsAllowedForFloatDoubleFormats}}
.type(Set.of(
Integer.class,
Float.class,
))
{{else}}
.type(Set.of(Float.class))
{{/if}}
.format("float")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ types: typing.FrozenSet[typing.Type] = frozenset({
{{/eq}}
{{#eq this "integer"}}
int,
float,
Copy link
Copy Markdown
Contributor Author

@spacether spacether Apr 20, 2024

Choose a reason for hiding this comment

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

Per json schema, int + float should be allowed in for int types. The values will be checked to ensure that they are integer like.
In the future a flag could be added floatingTypesAllowedForTypeInteger to disallow float here in python and Float/Double in java
The original code only allowing in int here is a bug.

{{/eq}}
{{#eq this "number"}}
float,
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/python/schemas/schemas.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,21 @@ class Int64Schema(IntSchema):

@dataclasses.dataclass(frozen=True)
class Float32Schema(NumberSchema):
{{#if generatorSettings.intsAllowedForFloatDoubleFormats}}
types: typing.FrozenSet[typing.Type] = frozenset({int, float})
{{else}}
types: typing.FrozenSet[typing.Type] = frozenset({float})
{{/if}}
format: str = 'float'


@dataclasses.dataclass(frozen=True)
class Float64Schema(NumberSchema):
{{#if generatorSettings.intsAllowedForFloatDoubleFormats}}
types: typing.FrozenSet[typing.Type] = frozenset({int, float})
{{else}}
types: typing.FrozenSet[typing.Type] = frozenset({float})
{{/if}}
format: str = 'double'


Expand Down