swagger-api / swagger-api/swagger-codegen

[R] Array JSON encoding doesn't work

Open
#8,612 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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

https://github.com/swagger-api/swagger-codegen/issues/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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the problem with the supplied Swagger declaration, the R generator command, and the generated Pet model's toJSONString() method. Trace the R generator template or entry point that emits array serialization, then verify that numeric and string arrays produce valid JSON without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.