-
Notifications
You must be signed in to change notification settings - Fork 6k
Fixed over-quoted examples for string definitions #5778
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
Conversation
|
@JFCote thanks for the fix. Am I correct in saying that the fix won't work with examples that contain double quote? |
|
|
||
| private void fixStringModel(ModelImpl m) { | ||
| if (m.getType() != null && m.getType().equals("string") && m.getExample() != null) { | ||
| m.setExample(m.getExample().toString().replace("\"", "")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @wing328 mentioned, this removes all the " from the example string, which likely is not what we want.
Could we restrict this to the first/last character?
Unfortunately, Swagger-models (or at least the classes/interfaces in use here) have no javadocs, so it is not really clear what should be in this example property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
Will take a little bit more time for my fix, I'm pretty busy right now! Just wanted to let you know I did not forget about this |
|
@JFCote I'll take a look tomorrow to see if there're other ways to address it |
…ble quote in the middle
|
There you go. It will only remote the double quote if they are at the beginning and the end of the example, when it's a string. Like I said, I did not run all of the sample .sh, I don't know how to do it all at once. |
|
@JFCote you could use Do we have a test case in the examples where it formerly was wrong? |
| String example = m.getExample().toString(); | ||
| if (example.substring(0, 2).equals("\"") && | ||
| example.substring(example.length() - 2).equals("\"")) { | ||
| m.setExample(example.substring(2, example.length() - 2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think your count is wrong here. The Java string "\"Hello\"" has length 7, not 9, and you need to cut off one character at each end, not two. Though please test this first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've test it and it works. And we want to cut "\"", which is 2 chars.
For example:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
\ | " | H | E | L | L | O | \ | "
First condition will return \" which is true
Second condition will return 9 - 2 = 7 -> \"
Then we simply remove everything between both which returns HELLO
I've test it and it works :) Let me know if it doesn't work in your case and I'll look into it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe I misunderstood what you are doing here ... I thought we have here a pair of " too much, which needs to be removed. (This is what the old replace code was doing, assuming there are no more quotation marks in the string.)
I thought the escaping (" → \") was only done after this step (while outputting).
Anyway, if you want to remove not just a single ", but the combination \", then you'll need to write it "\\\"" as a Java string literal (the single backslash is escaping the ").
Though I'll try it out instead of just using my Java knowledge to discuss things theoretically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JFCote I added the example snippet to src/main/resources/petstore.yaml, and regenerated the code (from your branch). The bug still occurs there. Using 1 instead of 2 fixes it.
(Feel free to pick those commits from my fork, or copy just the needed parts.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right... I really don't understand how it worked for me (tested it a couple of times...) but now, it doesn't work.
I will submit your fix in a couple of minutes..
|
@ePaul : I added you proposed changes to the PR. Thanks for the help and sorry again, I really don't know what happened during my testing! |
|
@ePaul and @wing328 : Is there something blocking this PR to be merged? I have tried to run the Let me know what I can do to help the merge of this PR. Thanks! |
|
@JFCote Looking at the code, I don't see anything blocking this. I can't build the project at my work computer currently (I'm stuck with an older maven installation), will try it later at home to see which changes actually come from this PR and which are already in master. |
|
@JFCote I'll take a look tomorrow. |
|
I just tried the sample-updating once on master (see #6004), and once again on the merge commit of your branch. Same result (apart from two markdown/txt files containing the build date). A question would be, could we have some examples so that there is actually a difference? |
|
@ePaul : Should I create a special petstore.yaml for this or modify the main yaml file that every generator use? |
|
@JFCote I think we can add a few to the existing petstore.yaml. I'll try to do it later and run some tests with your PR. |
| } | ||
| } | ||
|
|
||
| private void fixStringModel(ModelImpl m) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JFCote it would be nice to add a 1-liner or a proper docstring explaining what this function does.
… called PetStatus that test this scenario. Update sample only for JavaPlayFramework generator
|
Ok, I have added the comment + example in the |
|
I tried updating the other generators, no changes (which were not already changes before). (I guess they don't use the same YAML, or don't use the examples.) So 👍 from me. |
|
@wing328 Can you merge this when you have time? Thanks! |
|
@JFCote I'll do it tomorrow (sorry for the delay. too busy these days..) |
|
@wing328 No problem man! I completely understand! |
| type: string | ||
| message: | ||
| type: string | ||
| PetStatus: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JFCote I'll remove this tomorrow add the enum example to https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml instead.
When the Play generator, we should consider using petstore-with-fake-endpoints-models-for-testing.yaml, which covers a lot more edge cases.
|
Removed "PetStatus" via 943b47d |
PR checklist
./bin/to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.shand./bin/security/{LANG}-petstore.shif updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)2.3.0branch for breaking (non-backward compatible) changes.Description of the PR
This is quick fix for #5460
I don't know if it's a "good" way to deal with this but my understanding is that a "string" model is the exception to the rule because the example is not "json" but a simple string, so there is no need to double quote it.
I didn't ran the shell/batch script because I didn't know if there were a quick way to run them all at once... @wing328 , if the fix is accepted, can you run the appropriate script? Thanks!