swagger-api / swagger-api/swagger-codegen

[R] JSON serialization fails with object properties

Open
#8,610 2 comments 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

Attempting to JSON serialize a model containing a null property results in an R error, resulting in an empty string JSON output. Furthermore, it calls the wrong method (toJSON vs toJSONString), so non-nulls don't render properly.

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:
  Tag:
    type: "object"
  Pet:
    type: "object"
    properties:
      tag:
        $ref: "#/definitions/Tag"
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()$toJSONString()
Error in sprintf("{\n           \"tag\": %s\n        }", self$tag$toJSON()) : 
  attempt to apply non-function
Suggest a fix/enhancement
  • Null checks in JSON serialization.
  • Call toJSONString() on object fields, not toJSON(), or refactor more heavily.

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 failure with the generated Pet model using Pet$new()$toJSONString(), then inspect the generated R serialization path and the object-field calls shown in the error. Done means null properties serialize without an R error and non-null object properties use the working JSON-string method so the output is populated.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.