Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #450
Closed
Open
Issue created Jul 04, 2018 by Administrator@rootContributor

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
Assignee
Assign to
Time tracking