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

[R] Array JSON encoding doesn't work #8612

Open
calbach opened this issue Aug 21, 2018 · 1 comment
Open

[R] Array JSON encoding doesn't work #8612

calbach opened this issue Aug 21, 2018 · 1 comment

Comments

@calbach
Copy link

calbach commented Aug 21, 2018

Description

Cannot encode arrays in models to JSON.

Swagger-codegen version

swagger-codegen-cli-2.4.0-20180821.200234-311

Swagger declaration file content or url
swagger: "2.0"
info:
  description: ""
  title: "repro"
  version: "1"
paths:
  /pet:
    post:
      tags:
      - "pet"
      summary: "Add a new pet to the store"
      description: ""
      operationId: "addPet"
      parameters:
      - in: "body"
        name: "body"
        description: "Pet object that needs to be added to the store"
        required: true
        schema:
          $ref: "#/definitions/Pet"
      responses:
        200:
          description: ""
definitions:
  Pet:
    type: "object"
    properties:
      nums:
        type: array
        items:
          type: number
      strs:
        type: array
        items:
          type: string
Command line used for generation

java -jar swagger-codegen-cli-2.4.0-20180821.200234-311.jar generate --lang r -i swagger.yaml

Steps to reproduce
$ R
> library(devtools); install("."); library(swagger)
> Pet$new(nums=list(1, 2, 3))$toJSONString()
Error: is.character(x) is not TRUE
> Pet$new(strs=list('foo', 'bar'))$toJSONString()
character(0)
Related issues/PRs

#8610

Suggest a fix/enhancement

This generated code is clearly wrong and should be fixed:

    toJSONString = function() {
       sprintf(
        '{
           "nums": [%s],
           "strs": [%s]
        }',
        lapply(self$`nums`, function(x) paste(paste0('"', x, '"'), sep=",")),
        lapply(self$`strs`, function(x) paste(paste0('"', x, '"'), sep=","))
      )
    },

Firstly, paste0 doesn't work on non-strings. Next, the paste with "sep" should be outside the lapply.

@calbach
Copy link
Author

calbach commented Aug 27, 2018

Expected outputs (ignoring whitespace):

> Pet$new(nums=list(1, 2, 3))$toJSONString()
"{\"nums\": [1, 2, 3]}"

> Pet$new(strs=list('foo', 'bar'))$toJSONString()
"{\"strs\": [\"foo\", \"bar\"]}"

Again, unsure on whether missing values should be null or undefined in Swagger codegen JSON. The above assumes undefined, but if it's null, then the following would make sense:

> Pet$new(nums=list(1, 2, 3))$toJSONString()
"{\"nums\": [1, 2, 3], \"strs\": null}"

> Pet$new(strs=list('foo', 'bar'))$toJSONString()
"{\"nums\": null, \"strs\": [\"foo\", \"bar\"]}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant