openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open antssilva96 opened this issue 3 years ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] 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 avatar Aug 03 '22 14:08 antssilva96

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

spacether avatar Aug 08 '22 23:08 spacether

Hi @spacether ! I had some free time and decided to have a go. I opened https://github.com/OpenAPITools/openapi-generator/pull/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 avatar Aug 11 '22 20:08 antssilva96