Skip to content
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

[BUG][Java][Spring] Incorrect code generation with regular expressions #8625

Closed
5 of 6 tasks
jorgerod opened this issue Feb 5, 2021 · 7 comments
Closed
5 of 6 tasks

Comments

@jorgerod
Copy link
Contributor

jorgerod commented Feb 5, 2021

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

Using the java Spring generator, string regex patterns have forward slashes somehow additionally escaped, changing the meaning of the pattern.

openapi-generator version

Versions 4.3.1 and 5.0.0

OpenAPI declaration file content or URL
openapi: 3.0.2
...
components:
  parameters:
    ClientKey:
      name: clientKey
      in: header
      description: "Client key"
      schema:
        type: string
        pattern: '^(?!\s*$).+'
...
Generation Details

Generated line:

default ResponseEntity<Void> deleteTokens(@Pattern(regexp="^(?!\\s*$).+") @ApiParam(value = "User token")   String userToken) {​​​​​​​
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }​​​​​​​

Expected line:

default ResponseEntity<Void> deleteTokens(@Pattern(regexp="^(?!\s*$).+") @ApiParam(value = "User token")   String userToken) {​​​​​​​
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }​​​​​​​

As you can see, the pattern (@pattern(regexp="^(?!\\s*$).+") ) has one slash too many and modifies the regex.

@jorgerod jorgerod changed the title [BUG][Java][Spring] Spring server string pattern regex improperly escaped [BUG][Java][Spring] Incorrect code generation with regular expressions Feb 5, 2021
lavanya172 pushed a commit to lavanya172/openapi-generator that referenced this issue Feb 8, 2021
lavanya172 pushed a commit to lavanya172/openapi-generator that referenced this issue Feb 8, 2021
@cal101
Copy link
Contributor

cal101 commented Feb 10, 2021

@jorgerod Hi! You wrote "forward slashes" in your bug description but I guess you meant "backslash".

The generated code is java code where the actual Pattern is passed as a java string so the java string rules apply.
'\' is the escape char in strings and so is needed to escape the '\'.
That means "\\s" becomes the concatenation of '\' and 's' when seen by the Pattern constructor.
That means the duplicated backslash looks correct to me because it is needed by the representation which happens to be a java string.
Do you have an actual example that you expect to be matched but is not?

@lavanya172
Copy link

lavanya172 commented Feb 11, 2021

Regex pattern - "\s" matches the space, where as "\\s" matches the string "\s" (explanation: "\\" matches a "\" character and "s" matches a "s" character).

This is handled by avoid calling escapeText() for patterns in other generators.

References :
Issue #5973 PR #5974
Issue #1823 PR #2314

@cal101
Copy link
Contributor

cal101 commented Feb 11, 2021

I think it's important to distinguish between two issues here.

  1. The regular expression and it's meaning
  2. The notation form to express that expression in some environment. "What do I have to write to express the regular expression of 1."

Take the following example:

import javax.validation.constraints.Pattern;

import org.junit.Test;

public class RegexpTest {
    
    @Pattern(regexp = "\\s")
    @Test
    public void testPattern() throws Exception {
        String asString = "\\s";
        System.out.println("The string: >>" + asString + "<<, length=" + asString.length());

        final Pattern anno = getClass().getMethod("testPattern").getAnnotation(Pattern.class);
        System.out.println("The regexp: >>" + anno.regexp()+ "<<, length=" + anno.regexp().length());
    }
}

prints

The string: >>\s<<, length=2
The regexp: >>\s<<, length=2

To get the regular expression \s I have to write the java source code string "\\s" .

@cal101
Copy link
Contributor

cal101 commented Feb 11, 2021

I am open for discussions and any questions.
Regular expressions are complicated if you have to deal with one language but getting them right in the multitude of generated openapi generator languages is another level.

@lavanya172
Copy link

Yes, got it. Java accepts only the escaped string as input for Pattern regex to be valid, else will get an compile time error. So most likely this should not an issue.

@cal101
Copy link
Contributor

cal101 commented Feb 11, 2021

Yes, that's my opinion, too.
Sorry for your work on that issue.
Trying to solve non-issues feels bad.
But there is one or another issue left in this project.
Have fun!

@jorgerod
Copy link
Contributor Author

Hi
@cal101 You are absolutely right. Thank you for your help.

I close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants