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

[RUST] Implement escape reserved word for Rust Client codegen.

Open scottmmjackson opened this issue 1 year ago • 4 comments

The Bitbucket Cloud API has "ref" as a swagger symbol. Thus on master it fails with a reserved word error. After this change, this should work:

$ ./run-in-docker.sh generate -i https://dac-static.atlassian.com/cloud/bitbucket/swagger.v3.json?_v=2.300.22-0.1297.0 -g rust -o /gen/out/

@frol @farcaller @richardwhiuk @paladinzh @jacob-pro

PR checklist

  • [x] Read the contribution guidelines.
  • [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • [x] Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH) Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • [x] File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • [x] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

scottmmjackson avatar Feb 12 '24 13:02 scottmmjackson

#17795 and #17368 are likely fixed by this change

scottmmjackson avatar Feb 12 '24 13:02 scottmmjackson

I thought the Rust generator already implemented this - or am I missing something? This will need some test cases to demonstrate either way

https://github.com/OpenAPITools/openapi-generator/blob/ec42f88c6a27a0d422390ecdf5adb5785b2be5e7/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRustCodegen.java#L215-L223

jacob-pro avatar Feb 12 '24 13:02 jacob-pro

@jacob-pro I can definitely add some tests. #17795 is the issue this comes from, where escaping reserved words is not working.

This is probably because the abstract code you cited isn't the right place to escape. The DefaultCodegen implements it as a pure throw:

https://github.com/OpenAPITools/openapi-generator/blob/ec42f88c6a27a0d422390ecdf5adb5785b2be5e7/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L1760

This is why when attempting to use this generator, we get this in the stack trace:

Caused by: java.lang.RuntimeException: reserved word ref not allowed
	at org.openapitools.codegen.DefaultCodegen.escapeReservedWord(DefaultCodegen.java:1752)

BTW, based on your highlighting AbstractRustCodegen, I almost wonder if overriding the method in AbstractRustCodegen by moving the block you highlighted out into a separate member is the right choice here... thoughts?

scottmmjackson avatar Feb 12 '24 15:02 scottmmjackson

Been playing around with moving this to an override and it's proving kinda difficult. It seems like we have a variety of context-specific prefixes we're using, and escapeReservedWord doesn't know about context. I think this will end up having to change output, unless someone has a better idea.

scottmmjackson avatar Feb 13 '24 14:02 scottmmjackson

merged via https://github.com/OpenAPITools/openapi-generator/pull/17440 instead

thanks for the PR

wing328 avatar Mar 19 '24 10:03 wing328