Skip to content
Closed
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
20 changes: 14 additions & 6 deletions modules/openapi-generator/src/main/resources/r/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@
}
{{/vars}}
},
toJSONString = function() {
sprintf(
toJSONString = function() {
outstring = sprintf(
'{
{{#vars}}
"{{baseName}}":
Expand All @@ -181,24 +181,32 @@
{{/vars}}
}',
{{#vars}}
{{! Added the if else condition to handle null values}}
if (!is.null(self$`{{baseName}}`)) {
{{#isListContainer}}
{{#isPrimitiveType}}
paste(unlist(lapply(self$`{{{baseName}}}`, function(x) paste0('"', x, '"'))), collapse=","){{#hasMore}},{{/hasMore}}
paste(unlist(lapply(self$`{{{baseName}}}`, function(x) paste0('"', x, '"'))), collapse=",")
{{/isPrimitiveType}}
{{^isPrimitiveType}}
paste(unlist(lapply(self$`{{{baseName}}}`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=TRUE))), collapse=","){{#hasMore}},{{/hasMore}}
paste(unlist(lapply(self$`{{{baseName}}}`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=TRUE))), collapse=",")
{{/isPrimitiveType}}
{{/isListContainer}}
{{^isListContainer}}
{{#isPrimitiveType}}
self$`{{baseName}}`{{#hasMore}},{{/hasMore}}
self$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
jsonlite::toJSON(self$`{{baseName}}`$toJSON(), auto_unbox=TRUE){{#hasMore}},{{/hasMore}}
jsonlite::toJSON(self$`{{baseName}}`$toJSON(), auto_unbox=TRUE)
{{/isPrimitiveType}}
{{/isListContainer}}
}
else{
"null"
}{{#hasMore}},{{/hasMore}}
{{/vars}}
)
outstring = gsub("[\r\n]| ", "", outstring)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm afraid this won't work if the data/value contains line breaks

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@wing328 you are right, i can remove this line considering what you mentioned. Thanks

gsub(':\\[null\\]|:\\\"null\\\"',':null',outstring)
},
fromJSONString = function({{classname}}Json) {
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
Expand Down