Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

[BUG] Regex patterns without trailing slashes break Python client generation #11

Closed
5 of 6 tasks
antssilva96 opened this issue Aug 3, 2022 · 3 comments · Fixed by OpenAPITools/openapi-generator#13164 or #20
Closed
5 of 6 tasks
Assignees
Labels
bug Something isn't working
Milestone

Comments

@antssilva96
Copy link
Contributor

antssilva96 commented Aug 3, 2022

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When trying to generate a Python client SDK for a simple openAPI spec with an id with a given regex pattern, the generation fails with error Caused by: com.github.curiousoddman.rgxgen.parsing.dflt.RgxGenParseException: Unbalanced '{' - missing '}' at 'F0-9]{12' and (partial) stack trace

at com.github.curiousoddman.rgxgen.parsing.dflt.DefaultTreeBuilder.handleRepeatInCurvyBraces (DefaultTreeBuilder.java:479)
    at com.github.curiousoddman.rgxgen.parsing.dflt.DefaultTreeBuilder.handleRepeat (DefaultTreeBuilder.java:503)
    at com.github.curiousoddman.rgxgen.parsing.dflt.DefaultTreeBuilder.handleRepeatCharacter (DefaultTreeBuilder.java:314)
    at com.github.curiousoddman.rgxgen.parsing.dflt.DefaultTreeBuilder.parseGroup (DefaultTreeBuilder.java:249)
    at com.github.curiousoddman.rgxgen.parsing.dflt.DefaultTreeBuilder.build (DefaultTreeBuilder.java:664)
    at com.github.curiousoddman.rgxgen.parsing.dflt.DefaultTreeBuilder.get (DefaultTreeBuilder.java:673)
    at com.github.curiousoddman.rgxgen.RgxGen.<init> (RgxGen.java:71)
    at com.github.curiousoddman.rgxgen.RgxGen.<init> (RgxGen.java:62)
    at org.openapitools.codegen.languages.PythonClientCodegen.toExampleValueRecursive (PythonClientCodegen.java:1200)
    at org.openapitools.codegen.languages.PythonClientCodegen.toExampleValueRecursive (PythonClientCodegen.java:1128)
    at org.openapitools.codegen.languages.PythonClientCodegen.toExampleValue (PythonClientCodegen.java:1023)
    at org.openapitools.codegen.languages.PythonClientCodegen.setParameterExampleValue (PythonClientCodegen.java:1434)

This is not specific to the regex expression in my example and should happen with any regex that does not have ah extra char at the end that can be stripped without compromising the check that should be done.

openapi-generator version

6.0.0

OpenAPI declaration file content or url
---
openapi: 3.0.3
info:
  title: Test
  version: 1.0.0-SNAPSHOT
paths:
  /test:
    get:
      tags:
        - Test Resource
      parameters:
        - name: uuid
          in: query
          schema:
            $ref: '#/components/schemas/UUID'
      responses:
        "200":
          description: OK

components:
  schemas:
    UUID:
      format: uuid
      pattern: "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"
      type: string
Generation Details

Regular maven plugin generation with <generatorName>python</generatorName>

Steps to reproduce
  1. place the open api spec above somwhere the maven plugin can get it
  2. run mvn clean package in a pom.xml file with <inputSpec> linking to the file created in 1
  3. check the error that should be Unbalanced '{' - missing '}' at 'F0-9]{12'
Related issues/PRs
Suggest a fix

The problem seems to be with the REGEX in https://github.com/OpenAPITools/openapi-generator/blame/0c825ad3019e76b9d85e9883ef960a4643277c54/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java#L1179 where it should only strip of leading /, trailing / or trailing /i.

However, this REGEX is placing ANY final char into the second group, which means that in the case represented in this issue the main group will be [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12, because the final } is being captured as part of group 2.

Examples here: https://regex101.com/r/4YqK9h/1

I think what we want is something like ^/?(.+?)/?(i?)$ as shown in https://regex101.com/r/RAxPJU/1, where we really only capture trailing / or /i, not any last character that might be in the pattern.

@antssilva96 antssilva96 changed the title [BUG] UUID pattern breaks Python client generation [BUG] Regex patterns without trailing slashes break Python client generation Aug 3, 2022
@spacether
Copy link
Contributor

@antssilva96 thank you for the issue. Do you have time to submit a PR fixing it?

@antssilva96
Copy link
Contributor Author

Hi @spacether ! I had some free time and decided to have a go. I opened OpenAPITools/openapi-generator#13164

I left some concerns in the MR body, if you could check them out! Basically I'm unsure if the pattern that gets to that part of the code should always have the trailing and leading / appended or not. If so, that is another problem/bug that needs to be tackled and I'm not sure where.

In any case I think the regex a problem and is now fixed, but let me know your thoughts!

@antssilva96
Copy link
Contributor Author

antssilva96 commented Sep 26, 2022

Hello again spacether !

The most recent release broke this change: OpenAPITools/openapi-generator#13501

image

I think this is because this change should be applied to the PythonExperimental codegen as well (now renamed to be the PythonClientCodegen), no ?

(on the now called prior it has the change because at the time it was only fixed there)

image

Thanks!

@wing328 wing328 transferred this issue from OpenAPITools/openapi-generator Sep 26, 2022
@wing328 wing328 reopened this Sep 26, 2022
@spacether spacether added the bug Something isn't working label Sep 26, 2022
@spacether spacether self-assigned this Sep 27, 2022
@spacether spacether linked a pull request Sep 28, 2022 that will close this issue
3 tasks
@spacether spacether added this to the 1.0.0 milestone Sep 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.