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

Support multiple regexes for java ParameterType annotation #2924

Closed
bartsopers opened this issue Sep 18, 2024 · 5 comments
Closed

Support multiple regexes for java ParameterType annotation #2924

bartsopers opened this issue Sep 18, 2024 · 5 comments
Milestone

Comments

@bartsopers
Copy link

bartsopers commented Sep 18, 2024

🤔 What's the problem you're trying to solve?

I have a custom parameter type that resembles the built-in "string" parameter type. I want to allow enclosing the parameter in either single or double quotes. In the built-in "string" parameter type, this is achieved by using 2 regexes for the parameter type: one for eclosing it in single quotes and one for enclosing it in double quotes. However, the io.cucumber.java.ParameterType annotation only allows a single regex, although the io.cucumber.cucumberexpressions.ParameterType class does support multiple regexes.

✨ What's your proposed solution?

Change the return type of io.cucumber.java.ParameterType#value to String[] instead of String to allow multiple regexes to be provided to io.cucumber.cucumberexpressions.ParameterType

⛏ Have you considered any alternatives or workarounds?

No response

📚 Any additional context?

No response

@mpkorstanje mpkorstanje transferred this issue from cucumber/cucumber-expressions Sep 18, 2024
@mpkorstanje
Copy link
Contributor

Sounds reasonable.

It would strictly speaking be a breaking change, but I don't think anyone but Cucumber processes these annotations.

Would you be interested in sending a pull request?

@bartsopers
Copy link
Author

I tried, but I ran into some unexpected behavior where all capturing groups of all supplied regexes are passed as arguments to the @ParamaterType annotated method, whereas I would expect that only the capturing groups of the first matching regex would be passed as arguments. Idk whether this is intended behavior, but unfortunately I don't have enough time to diver deeper into it.

For now I worked around my issue by combining the two regexes into one.

@mpkorstanje
Copy link
Contributor

@mpkorstanje mpkorstanje added this to the v8.0.0 milestone Sep 25, 2024
@jkronegg
Copy link
Contributor

jkronegg commented Jan 31, 2025

@bartsopers
I'm not sure to understand the use-case. You would like something like this:

@ParameterType(value = {"\"[^\"]+\"", "\'[^\']+\'" })
public SomeObject lbaAccreditationStatus(String someString) {
    ... // convert someString into a SomeObject instance
}

If yes, this is not the only option, you could have multiple annotations:

@ParameterType("\"[^\"]+\"")
@ParameterType("\'[^\']+\'")

If I understand correctly, as a workaround, you combined both regexps to something like this:

@ParameterType("[\"\'][^\"\']+[\"\']")

If yes, it looks very simple and I'm not sure this really needed to change the framework.

@mpkorstanje I wrote a live-documentation plugin which process these @ParameterType annotations and generate a Markdown page which lists all possible parameter types.

@mpkorstanje
Copy link
Contributor

I've had a look into making it possible to match the first regular expression in a list.

In the cucumber-expression project when ever Argument.build is invoked the list of arguments and groups are reconciled. If a parameter type has multiple expressions, instead of using the group directly, it can use the children of that group. This does require that the groups created by CucumberExpression.rewriteParameter are capturing groups though.

But given the simplicity of the workaround I'll close this for now.

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

No branches or pull requests

3 participants