swagger-api / swagger-api/swagger-codegen

Byte length not calculated correctly in python

Open
#6,582 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated Python model setter shown in the issue and trace it back to the Python client-generation template or entry point. Reproduce the lte_subscription auth_key case with minLength 15 and maxLength 16, then verify the generated client accepts the 16-byte base64 value and rejects values outside the declared byte range.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.