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

?toString gives double double quotes on Strings, and no way to get unquoted string #1387

Open
FyiurAmron opened this issue Nov 9, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@FyiurAmron
Copy link

FyiurAmron commented Nov 9, 2023

Issue Description

If you provide a ?toString (i.e. FORMATTER_TO_STRING) on a string value, you'll get double double quotes, which makes no sense (at least I can't think of a single context where it would).
Also, it's impossible to e.g. process a directive accepting a string into an annotation with the string value passed verbatim. There is no ?omitStringQuotes (or ?noQuotes/?value/?raw etc.) or similar solution. A ValueMapper adds quotes by default for strings (via https://github.com/kobylynskyi/graphql-java-codegen/blob/main/src/main/java/com/kobylynskyi/graphql/codegen/mapper/ValueMapper.java#L108 ), so this prevents of using it literally e.g. to generate a Spring Boot SpEL value for @PreAuthorize, since any double quote will obviously break it. This can be worked around by using string concat, which consumes the quotes, but it still blocks the use of strings from directives in complex expressions etc., e.g. as a (part of) the annotation name or name of the annotation param, because the quotes will usually break everything, especially since there is physically no way to escape the final one.

Code-wise, it would be best if mapString could just return value.getValue() with no quotes, and then e.g. ValueFormatter could have its format method altered to accept an additional param isString, that would add quotes only once regardless of the ?toString, and that would not add it with ?omitStringQuotes.

Steps to Reproduce

  • create an annotation with a String/ID param
  • try to map that annotation param via {{param?toString}}
  • try to map that annotation param to an unquoted value to e.g. use it in a quoted expression (currently possible only with extensive concat magic)

Expected Result

  • Single double quotes both by default and with ?toString, e.g. @Annotation(value = "foobar") , for both "@Annotation(value = {{foobarParam}}" and "@Annotation(value = {{foobarParam?toString}})"
  • No double quotes if requested, e.g. @Annotation(value = "prefoobarExtra") , for e.g. "@Annotation(value = \"pre{{foobarParam?omitStringQuotes}}Extra\""

Actual Result

  • Double double quotes with ?toString, e.g. @Annotation(value = ""foobar"") , for "@Annotation(value = {{foobarParam?toString}})"
  • No easy way to get rid of the double quotes, e.g. @Annotation(value = "pre"foobar"Extra") , for mapping "@Annotation(value = \"pre{{foobarParam}}Extra\"") ; possible workaround ATM is to use string concatenation via e.g. "@Annotation(value = \"pre\"+{{foobarParam}}+\"Extra\"), but a) it's really ugly in the generated code and feels like more of a hack and not a normal use case, especially since the code could theoretically be generated without the need for such tricks easily, and b) it only works in the simplest cases, when the directive string value is consumed as a concatenable string finally, and not e.g. as a (part of) a name of annotation/annotation param/enum value etc.

Your Environment and Setup

  • graphql-java-codegen version: 5.8.0
  • Build tool: Gradle 8.4, Kotlin DSL
  • Mapping Config: irrelevant, see above
@FyiurAmron FyiurAmron added the bug Something isn't working label Nov 9, 2023
@FyiurAmron FyiurAmron changed the title ?toString gives double double quotes, and no way to get unquoted string ?toString gives double double quotes on Strings, and no way to get unquoted string Nov 10, 2023
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

No branches or pull requests

1 participant