Skip to content

fix(python-flask): validate byte length for format:byte fields#23177

Open
1cbyc wants to merge 1 commit intoOpenAPITools:masterfrom
1cbyc:fix-byte-length-validation
Open

fix(python-flask): validate byte length for format:byte fields#23177
1cbyc wants to merge 1 commit intoOpenAPITools:masterfrom
1cbyc:fix-byte-length-validation

Conversation

@1cbyc
Copy link

@1cbyc 1cbyc commented Mar 8, 2026

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


Summary by cubic

Fixes validation for format:byte fields in the Python Flask generator by checking the base64-decoded byte length for minLength/maxLength. Prevents incorrect passes/failures when validating byte arrays; non-byte fields still use string length checks.

  • Bug Fixes
    • Added isByteArray guards in model.mustache to validate decoded byte length for minLength and maxLength.
    • Skips enforcement on invalid base64 here so existing base64 validation can handle it.

Written for commit 25e4582. Summary will update on new commits.

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 OpenAPITools#450
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/python-flask/model.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/python-flask/model.mustache:142">
P2: Byte-array length validation silently skips invalid base64/type errors, allowing malformed `format:byte` values to bypass validation.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

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
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 8, 2026

Choose a reason for hiding this comment

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

P2: Byte-array length validation silently skips invalid base64/type errors, allowing malformed format:byte values to bypass validation.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/python-flask/model.mustache, line 142:

<comment>Byte-array length validation silently skips invalid base64/type errors, allowing malformed `format:byte` values to bypass validation.</comment>

<file context>
@@ -130,12 +130,38 @@ class {{classname}}(Model):
+                    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}}
</file context>
Suggested change
pass
raise ValueError("Invalid value for `{{name}}`, must be valid base64-encoded data") # noqa: E501
Fix with Cubic

Copy link
Member

Choose a reason for hiding this comment

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

thanks for the PR.

@1cbyc can you please review this feedback when you've time?

@1cbyc 1cbyc closed this Mar 8, 2026
@1cbyc 1cbyc reopened this Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Byte length not calculated correctly in python

2 participants