From 25e4582f216d849437d6c0f63e841fcb855e610d Mon Sep 17 00:00:00 2001 From: 1cbyc Date: Sun, 8 Mar 2026 11:33:36 +0000 Subject: [PATCH] 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 --- .../resources/python-flask/model.mustache | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/python-flask/model.mustache b/modules/openapi-generator/src/main/resources/python-flask/model.mustache index bf1818f6c104..5a541ef5a73c 100644 --- a/modules/openapi-generator/src/main/resources/python-flask/model.mustache +++ b/modules/openapi-generator/src/main/resources/python-flask/model.mustache @@ -130,12 +130,38 @@ class {{classname}}(Model): {{/required}} {{#hasValidation}} {{#maxLength}} +{{#isByteArray}} + if {{name}} is not None: + import base64 + try: + decoded_length = len(base64.b64decode({{name}})) + if decoded_length > {{maxLength}}: + raise ValueError("Invalid value for `{{name}}`, byte length must be less than or equal to `{{maxLength}}`") # noqa: E501 + except (TypeError, ValueError, base64.binascii.Error): + # If it's not valid base64, other validation will catch it + pass +{{/isByteArray}} +{{^isByteArray}} if {{name}} is not None and len({{name}}) > {{maxLength}}: raise ValueError("Invalid value for `{{name}}`, length must be less than or equal to `{{maxLength}}`") # noqa: E501 +{{/isByteArray}} {{/maxLength}} {{#minLength}} +{{#isByteArray}} + if {{name}} is not None: + import base64 + try: + decoded_length = len(base64.b64decode({{name}})) + if decoded_length < {{minLength}}: + raise ValueError("Invalid value for `{{name}}`, byte length must be greater than or equal to `{{minLength}}`") # noqa: E501 + except (TypeError, ValueError, base64.binascii.Error): + # If it's not valid base64, other validation will catch it + pass +{{/isByteArray}} +{{^isByteArray}} if {{name}} is not None and len({{name}}) < {{minLength}}: raise ValueError("Invalid value for `{{name}}`, length must be greater than or equal to `{{minLength}}`") # noqa: E501 +{{/isByteArray}} {{/minLength}} {{#maximum}} if {{name}} is not None and {{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: # noqa: E501