Byte length not calculated correctly in python
Created by: ugnom
Copying from original issue in swagger-codegen issue 6582 https://github.com/swagger-api/swagger-codegen/issues/6582
Description
In our swagger declaration, we defined a property of type string and format bytes. We also specified that it should be between 15 and 16 bytes in length, like "AAAAAAAAAAAAAAAAAAAAAA==". When using our service as a REST API and sending it through our swagger page, strings like the example before work correctly. However, when trying to use the python client, and send in the string "AAAAAAAAAAAAAAAAAAAAAA==", it checks the string character length (24), instead of the byte length (16). If we try to send in something other than a string, it fails the regular expression check. Basically, the python generated code is incorrectly checking the length of bytes by looking at string length.
Swagger-codegen version
2.2.3
Swagger declaration file content or url
lte_subscription:
type: object
required:
- auth_key
properties:
auth_key:
type: string
format: byte
x-nullable: true
minLength: 15
maxLength: 16
example: "AAAAAAAAAAAAAAAAAAAAAA=="
Command line used for generation
Generated into python
Steps to reproduce
if you call
import swagger_client
lte = swagger_client.LteSubscription(auth_key="AAAAAAAAAAAAAAAAAAAAAA==")
Related issues/PRs
Checked, and couldn't find anything
Suggest a fix/enhancement
in the generated code, it currently is:
@auth_key.setter
def auth_key(self, auth_key):
"""
Sets the auth_key of this LteSubscription.
:param auth_key: The auth_key of this LteSubscription.
:type: str
"""
if auth_key is None:
raise ValueError("Invalid value for `auth_key`, must not be `None`")
if auth_key is not None and len(auth_key) > 16:
raise ValueError("Invalid value for `auth_key`, length must be less than or equal to `16`")
if auth_key is not None and len(auth_key) < 15:
raise ValueError("Invalid value for `auth_key`, length must be greater than or equal to `15`")
if auth_key is not None and not re.search('^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$', auth_key):
raise ValueError("Invalid value for `auth_key`, must be a follow pattern or equal to `/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/`")
self._auth_key = auth_key
but it should be this. Considering it requires a base64 encoded string, it should check the length of the base 64 decoding.
import base64
@auth_key.setter
def auth_key(self, auth_key):
"""
Sets the auth_key of this LteSubscription.
:param auth_key: The auth_key of this LteSubscription.
:type: str
"""
param_length = len(base64.b64decode(auth_key))
if auth_key is None:
raise ValueError("Invalid value for `auth_key`, must not be `None`")
if auth_key is not None and len(param_length) > 16:
raise ValueError("Invalid value for `auth_key`, byte length must be less than or equal to `16`")
if auth_key is not None and len(param_length) < 15:
raise ValueError("Invalid value for `auth_key`, byte length must be greater than or equal to `15`")
if auth_key is not None and not re.search('^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$', auth_key):
raise ValueError("Invalid value for `auth_key`, must be a follow pattern or equal to `/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/`")
self._auth_key = auth_key