OpenAPITools / OpenAPITools/openapi-generator

[BUG][PYTHON] boolean ref6570 expansion

Open
#13,686 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python duplicate Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

In the _ref6570_expansion booleans are consciously not covered, while they should be included as "true" or "false".

Unfortunately, I cannot share the spec of the api. The parameters of a GET request included
{ "name": "with_data", "type": "boolean", "description": "...", "in": "query", "required": false, "default": false },

Suggest a fix

Just had to patch two sections:

class ParameterSerializerBase:
    @classmethod
    def _get_default_explode(cls, style: ParameterStyle) -> bool:
        return False

    @staticmethod
    def __ref6570_item_value(in_data: typing.Any, percent_encode: bool):
        """
        Get representation if str/float/int/None/items in list/ values in dict
        None is returned if an item is undefined, use cases are value=
        - None
        - []
        - {}
        - [None, None None]
        - {'a': None, 'b': None}
        """
        if type(in_data) in {str, float, int}:
            if percent_encode:
                return quote(str(in_data))
            return str(in_data)
        elif isinstance(in_data, bool): # HERE
            return "true" if in_data else "false" # HERE
        elif isinstance(in_data, none_type):
            # ignored by the expansion process https://datatracker.ietf.org/doc/html/rfc6570#section-3.2.1
            return None
        elif isinstance(in_data, list) and not in_data:
            # ignored by the expansion process https://datatracker.ietf.org/doc/html/rfc6570#section-3.2.1
            return None
        elif isinstance(in_data, dict) and not in_data:
            # ignored by the expansion process https://datatracker.ietf.org/doc/html/rfc6570#section-3.2.1
            return None
        raise ApiValueError(
            "Unable to generate a ref6570 item representation of {}".format(in_data)
        )

[...]

    @classmethod
    def _ref6570_expansion(
        cls,
        variable_name: str,
        in_data: typing.Any,
        explode: bool,
        percent_encode: bool,
        prefix_separator_iterator: PrefixSeparatorIterator,
    ) -> str:
        """
        Separator is for separate variables like dict with explode true, not for array item separation
        """
        named_parameter_expansion = prefix_separator_iterator.separator in {"&", ";"}
        var_name_piece = variable_name if named_parameter_expansion else ""
        if type(in_data) in {str, float, int, bool}: # add ", bool" here.
            return cls.__ref6570_str_float_int_expansion(
                variable_name,
                in_data,
                explode,
                percent_encode,
                prefix_separator_iterator,
                var_name_piece,
                named_parameter_expansion,
            )

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

Read modules/openapi-generator/src/main/resources/python/api_client.handlebars, focusing on ParameterSerializerBase and the _ref6570_expansion sections identified in the issue. Use the boolean query-parameter example to inspect the generated Python client's serialization behavior; done means boolean values expand as "true" or "false".

Written by the indexing model from the issue text.

Assessment

Tech stack
handlebars, python
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.