Skip to content

Invalid identifiers generated #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kalzoo opened this issue Sep 30, 2020 · 3 comments · Fixed by #206
Closed

Invalid identifiers generated #203

kalzoo opened this issue Sep 30, 2020 · 3 comments · Fixed by #206
Labels
🐞bug Something isn't working
Milestone

Comments

@kalzoo
Copy link
Contributor

kalzoo commented Sep 30, 2020

Describe the bug

Schema fields may generate client models which contain invalid identifiers.

Example

    Foo:
      additionalProperties: false
      properties:
        1Q:
          title: 1Q
          type: object
        2Q:
          title: 2Q
          type: object
      required:
        - 1Q
        - 2Q
      title: Foo
      type: object

generates a class which is invalid Python:

class Foo:
    """  """
    1_q: Dict[Any, Any]
    2_q: Dict[Any, Any]

    def to_dict(self) -> Dict[str, Any]:
        1_q = self.1_q

        2_q = self.2_q


        return {
            "1Q": 1_q,
            "2Q": 2_q,
        }

Expected behavior

The output should be sanitized in some way. Python's str.isidentifier can help, and the output could be prefixed by _ (although that could appear to be private, and thus confusing), or __, or field_, or something else.

In our manually-written clients, we replace it with one_q and two_q, but that's not generally applicable in openapi-python-client (for reasons to include internationalization and maintaining semantic meaning)

As it stands now, this requires manual correction after generation. If the intent is (eventually) to support all valid OpenAPI schemas, then this would have to be addressed at some point.

Desktop (please complete the following information):

  • OS: OSX 10.15.6
  • Python Version: 3.7.3
  • openapi-python-client version: 0.6.1

Thanks! Happy to send a PR for it.

@kalzoo kalzoo added the 🐞bug Something isn't working label Sep 30, 2020
@dbanty
Copy link
Collaborator

dbanty commented Sep 30, 2020

Thanks @kalzoo, this is definitely something we should address. I do like the field_ option better to avoid the Python private syntax. If you want to take a crack at it, I'd be happy to accept a PR. I believe this would just be some additional sanitization on parser.properties.Property.python_name

@dbanty
Copy link
Collaborator

dbanty commented Oct 7, 2020

Thanks for the report and the fix for this @kalzoo! It'll be in the next release (0.6.2). My plan is to wait until a couple more things are complete before doing a release, but if you need this soon just let me know and I'll put the release together sooner.

@kalzoo
Copy link
Contributor Author

kalzoo commented Oct 8, 2020

That works for me! The patching setup I have is good for the short term. Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants