Skip to content

Commit 25e4582

Browse files
committed
fix(python-flask): validate byte length for format:byte fields
When a string field has format:byte with minLength/maxLength constraints, the generated validation code incorrectly checks string length instead of base64-decoded byte length. Added conditional logic using isByteArray flag to validate decoded byte length for byte array fields. Maintains existing string length validation for non-byte fields. Fixes #450
1 parent cca5dda commit 25e4582

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

  • modules/openapi-generator/src/main/resources/python-flask

modules/openapi-generator/src/main/resources/python-flask/model.mustache

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,38 @@ class {{classname}}(Model):
130130
{{/required}}
131131
{{#hasValidation}}
132132
{{#maxLength}}
133+
{{#isByteArray}}
134+
if {{name}} is not None:
135+
import base64
136+
try:
137+
decoded_length = len(base64.b64decode({{name}}))
138+
if decoded_length > {{maxLength}}:
139+
raise ValueError("Invalid value for `{{name}}`, byte length must be less than or equal to `{{maxLength}}`") # noqa: E501
140+
except (TypeError, ValueError, base64.binascii.Error):
141+
# If it's not valid base64, other validation will catch it
142+
pass
143+
{{/isByteArray}}
144+
{{^isByteArray}}
133145
if {{name}} is not None and len({{name}}) > {{maxLength}}:
134146
raise ValueError("Invalid value for `{{name}}`, length must be less than or equal to `{{maxLength}}`") # noqa: E501
147+
{{/isByteArray}}
135148
{{/maxLength}}
136149
{{#minLength}}
150+
{{#isByteArray}}
151+
if {{name}} is not None:
152+
import base64
153+
try:
154+
decoded_length = len(base64.b64decode({{name}}))
155+
if decoded_length < {{minLength}}:
156+
raise ValueError("Invalid value for `{{name}}`, byte length must be greater than or equal to `{{minLength}}`") # noqa: E501
157+
except (TypeError, ValueError, base64.binascii.Error):
158+
# If it's not valid base64, other validation will catch it
159+
pass
160+
{{/isByteArray}}
161+
{{^isByteArray}}
137162
if {{name}} is not None and len({{name}}) < {{minLength}}:
138163
raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`") # noqa: E501
164+
{{/isByteArray}}
139165
{{/minLength}}
140166
{{#maximum}}
141167
if {{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: # noqa: E501

0 commit comments

Comments
 (0)