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

[ASP.NET Core] Added support for Operation EXAMPLE value #7091

Merged
merged 2 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,20 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
}
}

if (operation.examples != null){
for (Map<String, String> example : operation.examples)
{
for (Map.Entry<String, String> entry : example.entrySet())
{
// Replace " with \", \r, \n with \\r, \\n
String val = entry.getValue().replace("\"", "\\\"")
.replace("\r","\\r")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few issues with this approach that I see…

  1. This will replace unexpected results. For instance, what if my example was:
     { 
         "example_commands": [
             "\run", "\nip", "\yap", "\rollover"
         ],
         "fast_commands": [
             "\\run", "\\rollover"
          ],
         "omg_slow_down_puppy": [
              "\\\run", "\\\rollover"
          ],
         "search_command" : "\\"
     }
    
  2. This will lead to failed compilation if we don't escape other things like double-quotes and format placeholders like {0}, {0,15}, {0:t}, etc.
  3. afiak this will result in multiline strings, regardless of how many times you escape these characters. I could be wrong on that, though.

The amount of effort to resolve this is, I don't think, worth the benefit.

Some of this could be resolved by replacing only on boundaries, for example a regex like \b\\r\b which would catch probably most cases outside of text occurrences:

this\r"matches
this\rdoesn't

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestions @jimschubert!
On the double quotes.. I do replace these as well..

I'm not that fluent in Java..
How would I rewrite the code to do regex replace instead?

Thanks!

.replace("\n","\\n");
entry.setValue(val);
}
}
}

processOperation(operation);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ namespace {{packageName}}.Controllers
{{/dataType}}{{/responses}}
{{#returnType}}
string exampleJson = null;
{{#examples}}
exampleJson = "{{{example}}}";
{{/examples}}
{{#isListCollection}}{{>listReturn}}{{/isListCollection}}{{^isListCollection}}{{#isMapContainer}}{{>mapReturn}}{{/isMapContainer}}{{^isMapContainer}}{{>objectReturn}}{{/isMapContainer}}{{/isListCollection}}
{{!TODO: defaultResponse, examples, auth, consumes, produces, nickname, externalDocs, imports, security}}
//TODO: Change the data returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public virtual IActionResult FindPetsByStatus([FromQuery][Required()]List<string
// return StatusCode(400);

string exampleJson = null;
exampleJson = "<Pet>\n <id>123456789</id>\n <name>doggie</name>\n <photoUrls>\n <photoUrls>aeiou</photoUrls>\n </photoUrls>\n <tags>\n </tags>\n <status>aeiou</status>\n</Pet>";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way to ONLY pick JSON examples here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a hack, but iterate responses in the generator code and remove them if they don't start with a "{". If you go that route, is suggest logging a warning any time an example is dropped by the generator in this way.

Mustache doesn't really have logic. It does allow custom filters, so maybe we could write a filter that will only write a value of it is valid json. An issue with that approach is that you can't pick "the first json example* like you could in The generator processing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a discussion to add an isJson tag before but no one seems to have time to implement that yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a tag rather than Mustache filter? just curious.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh. I get what you're saying. It would be weird to use a tag to get a single json object from ab list of mixed possible types, I think.

exampleJson = "[ {\n \"tags\" : [ {\n \"id\" : 1,\n \"name\" : \"name\"\n }, {\n \"id\" : 1,\n \"name\" : \"name\"\n } ],\n \"id\" : 0,\n \"category\" : {\n \"id\" : 6,\n \"name\" : \"name\"\n },\n \"status\" : \"available\",\n \"name\" : \"doggie\",\n \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ]\n}, {\n \"tags\" : [ {\n \"id\" : 1,\n \"name\" : \"name\"\n }, {\n \"id\" : 1,\n \"name\" : \"name\"\n } ],\n \"id\" : 0,\n \"category\" : {\n \"id\" : 6,\n \"name\" : \"name\"\n },\n \"status\" : \"available\",\n \"name\" : \"doggie\",\n \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ]\n} ]";

var example = exampleJson != null
? JsonConvert.DeserializeObject<List<Pet>>(exampleJson)
Expand Down Expand Up @@ -122,6 +124,8 @@ public virtual IActionResult FindPetsByTags([FromQuery][Required()]List<string>
// return StatusCode(400);

string exampleJson = null;
exampleJson = "<Pet>\n <id>123456789</id>\n <name>doggie</name>\n <photoUrls>\n <photoUrls>aeiou</photoUrls>\n </photoUrls>\n <tags>\n </tags>\n <status>aeiou</status>\n</Pet>";
exampleJson = "[ {\n \"tags\" : [ {\n \"id\" : 1,\n \"name\" : \"name\"\n }, {\n \"id\" : 1,\n \"name\" : \"name\"\n } ],\n \"id\" : 0,\n \"category\" : {\n \"id\" : 6,\n \"name\" : \"name\"\n },\n \"status\" : \"available\",\n \"name\" : \"doggie\",\n \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ]\n}, {\n \"tags\" : [ {\n \"id\" : 1,\n \"name\" : \"name\"\n }, {\n \"id\" : 1,\n \"name\" : \"name\"\n } ],\n \"id\" : 0,\n \"category\" : {\n \"id\" : 6,\n \"name\" : \"name\"\n },\n \"status\" : \"available\",\n \"name\" : \"doggie\",\n \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ]\n} ]";

var example = exampleJson != null
? JsonConvert.DeserializeObject<List<Pet>>(exampleJson)
Expand Down Expand Up @@ -157,6 +161,8 @@ public virtual IActionResult GetPetById([FromRoute][Required]long? petId)
// return StatusCode(404);

string exampleJson = null;
exampleJson = "<Pet>\n <id>123456789</id>\n <name>doggie</name>\n <photoUrls>\n <photoUrls>aeiou</photoUrls>\n </photoUrls>\n <tags>\n </tags>\n <status>aeiou</status>\n</Pet>";
exampleJson = "{\n \"tags\" : [ {\n \"id\" : 1,\n \"name\" : \"name\"\n }, {\n \"id\" : 1,\n \"name\" : \"name\"\n } ],\n \"id\" : 0,\n \"category\" : {\n \"id\" : 6,\n \"name\" : \"name\"\n },\n \"status\" : \"available\",\n \"name\" : \"doggie\",\n \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ]\n}";

var example = exampleJson != null
? JsonConvert.DeserializeObject<Pet>(exampleJson)
Expand Down Expand Up @@ -232,6 +238,7 @@ public virtual IActionResult UploadFile([FromRoute][Required]long? petId, [FromF
// return StatusCode(200, default(ApiResponse));

string exampleJson = null;
exampleJson = "{\n \"message\" : \"message\",\n \"code\" : 0,\n \"type\" : \"type\"\n}";

var example = exampleJson != null
? JsonConvert.DeserializeObject<ApiResponse>(exampleJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public virtual IActionResult GetInventory()
// return StatusCode(200, default(Dictionary<string, int?>));

string exampleJson = null;
exampleJson = "{\n \"key\" : 0\n}";

var example = exampleJson != null
? JsonConvert.DeserializeObject<Dictionary<string, int?>>(exampleJson)
Expand Down Expand Up @@ -105,6 +106,8 @@ public virtual IActionResult GetOrderById([FromRoute][Required][Range(1, 5)]long
// return StatusCode(404);

string exampleJson = null;
exampleJson = "<Order>\n <id>123456789</id>\n <petId>123456789</petId>\n <quantity>123</quantity>\n <shipDate>2000-01-23T04:56:07.000Z</shipDate>\n <status>aeiou</status>\n <complete>true</complete>\n</Order>";
exampleJson = "{\n \"id\" : 0,\n \"petId\" : 6,\n \"complete\" : false,\n \"status\" : \"placed\",\n \"quantity\" : 1,\n \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\"\n}";

var example = exampleJson != null
? JsonConvert.DeserializeObject<Order>(exampleJson)
Expand Down Expand Up @@ -135,6 +138,8 @@ public virtual IActionResult PlaceOrder([FromBody]Order body)
// return StatusCode(400);

string exampleJson = null;
exampleJson = "<Order>\n <id>123456789</id>\n <petId>123456789</petId>\n <quantity>123</quantity>\n <shipDate>2000-01-23T04:56:07.000Z</shipDate>\n <status>aeiou</status>\n <complete>true</complete>\n</Order>";
exampleJson = "{\n \"id\" : 0,\n \"petId\" : 6,\n \"complete\" : false,\n \"status\" : \"placed\",\n \"quantity\" : 1,\n \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\"\n}";

var example = exampleJson != null
? JsonConvert.DeserializeObject<Order>(exampleJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public virtual IActionResult GetUserByName([FromRoute][Required]string username)
// return StatusCode(404);

string exampleJson = null;
exampleJson = "<User>\n <id>123456789</id>\n <username>aeiou</username>\n <firstName>aeiou</firstName>\n <lastName>aeiou</lastName>\n <email>aeiou</email>\n <password>aeiou</password>\n <phone>aeiou</phone>\n <userStatus>123</userStatus>\n</User>";
exampleJson = "{\n \"id\" : 0,\n \"lastName\" : \"lastName\",\n \"phone\" : \"phone\",\n \"username\" : \"username\",\n \"email\" : \"email\",\n \"userStatus\" : 6,\n \"firstName\" : \"firstName\",\n \"password\" : \"password\"\n}";

var example = exampleJson != null
? JsonConvert.DeserializeObject<User>(exampleJson)
Expand Down Expand Up @@ -169,6 +171,8 @@ public virtual IActionResult LoginUser([FromQuery][Required()]string username, [
// return StatusCode(400);

string exampleJson = null;
exampleJson = "aeiou";
exampleJson = "\"\"";

var example = exampleJson != null
? JsonConvert.DeserializeObject<string>(exampleJson)
Expand Down