OpenAPITools / OpenAPITools/openapi-generator

[BUG][R] Boolean values garbled in toJSONString() as quoted Strings and uppercase

Open
#9,468 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generated code seems to not treat boolean values correctly in toJSON() serialisation.
It is currently turned into "use_datacite":"TRUE", causing a 400 Bad Request with "detail": "'TRUE' is not of type 'boolean'" . Just lower-casing "use_datacite":"true"also causes
"detail": "'true' is not of type 'boolean' ".

=> The client has to send the boolean values as "use_datacite":true

openapi-generator version

Using 5.2.0-SNAPSHOT, unsure if that ever worked and is a regression now.

OpenAPI declaration file content or url

The OpenAPI in question is available from https://raw.githubusercontent.com/sneumann/fuji/patch-1/fuji_server/yaml/swagger.yaml

Generation Details

I created an R package with
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i https://raw.githubusercontent.com/sneumann/fuji/patch-1/fuji_server/yaml/swagger.yaml -g r --package-name rfuji -o /local/rfuji
(5.2.0-SNAPSHOT, with docker hash 0656cdcfe71e)

Steps to reproduce

After R CMD INSTALL /tmp/rfuji I do

library(rfuji)
Body <- rfuji::Body$new()
Body$use_datacite <- T
cat(Body$toJSONString())
# {"test_debug": "FALSE", "use_datacite": "TRUE" }

In a patched version of the package I get the correct:

{"test_debug": false, "use_datacite": true }
Related issues/PRs

None close enough I could find.

Suggest a fix

The generated file rfuji/R/body.R corresponds to the parameter body
in https://github.com/pangaea-data-publisher/fuji/blob/master/fuji_server/yaml/swagger.yaml#L560

and has a boolean called use_datacite:
https://github.com/pangaea-data-publisher/fuji/blob/master/fuji_server/yaml/swagger.yaml#L578

The generated code turns this boolean into a character string, which the REST service does not like, returning 400 Bad Request with "detail": "'TRUE' is not of type 'boolean'"

Body <- R6::R6Class( 'Body', public = list(
    toJSONString = function() {
      jsoncontent <- c(
...
        if (!is.null(self$`test_debug`)) {
             sprintf( ' "test_debug": "%s" ', self$`test_debug` )
        },
        if (!is.null(self$`use_datacite`)) {
        sprintf(' "use_datacite": "%s" ', self$`use_datacite` )}
...

My locally fixed package has instead removed the quotes in "%s" and added tolower():

       if (!is.null(self$`use_datacite`)) {
           sprintf('"use_datacite": %s', tolower(self$`use_datacite`)
        )}

This must probably be handled in the templates such as
https://github.com/OpenAPITools/openapi-generator/blob/60dcf8613f6b75b606757c4a991fe018ad10ee99/modules/openapi-generator/src/main/resources/r/model.mustache#L172
by testing for boolean and then emitting the above corrected code.

Hope that helped,
yours,
Steffen

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

Start with modules/openapi-generator/src/main/resources/r/model.mustache around the referenced line, then reproduce generation using the supplied swagger.yaml and inspect the generated rfuji/R/body.R. Compare Body$toJSONString() output for use_datacite with the expected unquoted JSON boolean; done means generated R models serialize boolean values as JSON booleans rather than quoted strings.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.