-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[python-nextgen] optionally support float strict type #14618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,13 +46,14 @@ public class PythonNextgenClientCodegen extends AbstractPythonCodegen implements | |
public static final String PACKAGE_URL = "packageUrl"; | ||
public static final String DEFAULT_LIBRARY = "urllib3"; | ||
public static final String RECURSION_LIMIT = "recursionLimit"; | ||
public static final String PYTHON_ATTR_NONE_IF_UNSET = "pythonAttrNoneIfUnset"; | ||
public static final String FLOAT_STRICT_TYPE = "floatStrictType"; | ||
|
||
protected String packageUrl; | ||
protected String apiDocPath = "docs" + File.separator; | ||
protected String modelDocPath = "docs" + File.separator; | ||
protected boolean hasModelsToImport = Boolean.FALSE; | ||
protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup | ||
protected boolean floatStrictType = true; | ||
|
||
protected Map<Character, String> regexModifiers; | ||
|
||
|
@@ -164,6 +165,8 @@ public PythonNextgenClientCodegen() { | |
cliOptions.add(new CliOption(CodegenConstants.SOURCECODEONLY_GENERATION, CodegenConstants.SOURCECODEONLY_GENERATION_DESC) | ||
.defaultValue(Boolean.FALSE.toString())); | ||
cliOptions.add(new CliOption(RECURSION_LIMIT, "Set the recursion limit. If not set, use the system default value.")); | ||
cliOptions.add(new CliOption(FLOAT_STRICT_TYPE, "Use strict type for float, i.e. StrictFloat or confloat(strict=true, ...)") | ||
.defaultValue(Boolean.TRUE.toString())); | ||
|
||
supportedLibraries.put("urllib3", "urllib3-based client"); | ||
supportedLibraries.put("asyncio", "asyncio-based client"); | ||
|
@@ -259,6 +262,10 @@ public void processOpts() { | |
additionalProperties.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, useOneOfDiscriminatorLookup); | ||
} | ||
|
||
if (additionalProperties.containsKey(FLOAT_STRICT_TYPE)) { | ||
setFloatStrictType(convertPropertyToBooleanAndWriteBack(FLOAT_STRICT_TYPE)); | ||
} | ||
|
||
String modelPath = packagePath() + File.separatorChar + modelPackage.replace('.', File.separatorChar); | ||
String apiPath = packagePath() + File.separatorChar + apiPackage.replace('.', File.separatorChar); | ||
|
||
|
@@ -424,7 +431,6 @@ private String getPydanticType(CodegenParameter cp, | |
if (cp.hasValidation) { | ||
List<String> fieldCustomization = new ArrayList<>(); | ||
// e.g. confloat(ge=10, le=100, strict=True) | ||
fieldCustomization.add("strict=True"); | ||
if (cp.getMaximum() != null) { | ||
if (cp.getExclusiveMaximum()) { | ||
fieldCustomization.add("gt=" + cp.getMaximum()); | ||
|
@@ -443,12 +449,20 @@ private String getPydanticType(CodegenParameter cp, | |
fieldCustomization.add("multiple_of=" + cp.getMultipleOf()); | ||
} | ||
|
||
if (floatStrictType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code path is used for json schema number (float + int) and float only processing. Number should allow both in. How about:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's an option so users can choose whether they want float in strict type or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right but that option should only apply to floats right? The way it is now looks like you are applying float strict when the spec has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. - number: confloat(strict=True, le=543.2, ge=32.1) = ...
- float: Optional[confloat(strict=True, le=987.6, ge=54.3)] = None
- double: Optional[confloat(strict=True, le=123.4, ge=67.8)] = None
+ number: confloat(le=543.2, ge=32.1) = ...
+ float: Optional[confloat(le=987.6, ge=54.3)] = None
+ double: Optional[confloat(le=123.4, ge=67.8)] = None As illustrated in the sample diff as part of this change, the option (when set to false) simply removes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you applying strict float to type number with no format? Won't it throw a validation error if an integer input is given to it? Integer input is valid for that definition per There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's a user decision - they can switch on or off strict mode via an option. You can find more about why some users prefer strict mode in https://docs.pydantic.dev/blog/pydantic-v2/#strict-mode. |
||
fieldCustomization.add("strict=True"); | ||
} | ||
|
||
pydanticImports.add("confloat"); | ||
return String.format(Locale.ROOT, "%s(%s)", "confloat", | ||
StringUtils.join(fieldCustomization, ", ")); | ||
} else { | ||
pydanticImports.add("StrictFloat"); | ||
return "StrictFloat"; | ||
if (floatStrictType) { | ||
pydanticImports.add("StrictFloat"); | ||
return "StrictFloat"; | ||
} else { | ||
return "float"; | ||
} | ||
} | ||
} else if (cp.isInteger || cp.isLong || cp.isShort || cp.isUnboundedInteger) { | ||
if (cp.hasValidation) { | ||
|
@@ -645,7 +659,6 @@ private String getPydanticType(CodegenProperty cp, | |
if (cp.hasValidation) { | ||
List<String> fieldCustomization = new ArrayList<>(); | ||
// e.g. confloat(ge=10, le=100, strict=True) | ||
fieldCustomization.add("strict=True"); | ||
if (cp.getMaximum() != null) { | ||
if (cp.getExclusiveMaximum()) { | ||
fieldCustomization.add("lt=" + cp.getMaximum()); | ||
|
@@ -664,12 +677,20 @@ private String getPydanticType(CodegenProperty cp, | |
fieldCustomization.add("multiple_of=" + cp.getMultipleOf()); | ||
} | ||
|
||
if (floatStrictType) { | ||
fieldCustomization.add("strict=True"); | ||
} | ||
|
||
pydanticImports.add("confloat"); | ||
return String.format(Locale.ROOT, "%s(%s)", "confloat", | ||
StringUtils.join(fieldCustomization, ", ")); | ||
} else { | ||
pydanticImports.add("StrictFloat"); | ||
return "StrictFloat"; | ||
if (floatStrictType) { | ||
pydanticImports.add("StrictFloat"); | ||
return "StrictFloat"; | ||
} else { | ||
return "float"; | ||
} | ||
} | ||
} else if (cp.isInteger || cp.isLong || cp.isShort || cp.isUnboundedInteger) { | ||
if (cp.hasValidation) { | ||
|
@@ -1316,4 +1337,8 @@ public String escapeReservedWord(String name) { | |
} | ||
return "var_" + name; | ||
} | ||
|
||
public void setFloatStrictType(boolean floatStrictType) { | ||
this.floatStrictType = floatStrictType; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1521,7 +1521,6 @@ components: | |
type: object | ||
required: | ||
- number | ||
- byte | ||
- date | ||
- password | ||
properties: | ||
|
Uh oh!
There was an error while loading. Please reload this page.