openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] R generator handles enums in response payloads incorrectly

Open jlorince opened this issue 1 year ago • 1 comments

Bug Report Checklist

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

When a response payload contains an enum value, the R generator seems to produce unusable code.

The code to generate response payload from JSON ends up looking like this:

    fromJSON = function(input_json) {
      this_object <- jsonlite::fromJSON(input_json)
      ...
      if (!is.null(this_object$`status`)) {
        status_object <- ProjectRunStatus$new()
        status_object$fromJSON(jsonlite::toJSON(this_object$status, auto_unbox = TRUE, digits = NA))
        self$`status` <- status_object
      }
      ...

However, the line status_object <- ProjectRunStatus$new() fails because the class for the enumerated value cannot be initialized without a value:

    initialize = function(...) {
      local.optional.var <- list(...)
      val <- unlist(local.optional.var)
      enumvec <- .parse_ProjectRunStatus()
      stopifnot(length(val) == 1L)

      if (!val %in% enumvec)
          stop("Use one of the valid values: ",
              paste0(enumvec, collapse = ", "))
      private$value <- val
    },
openapi-generator version

6.1.0

Generation Details

yarn run openapi-generator-cli generate -i ./generated/openapi.json -o ../../r-sdk --package-name hexApi -g r

Steps to reproduce

See Above

Related issues/PRs
Suggest a fix

I'm not too familiar with classes in R, so I'm not certain, but somehow the code should somehow be updated to not try to instantiate an empty enum class.

jlorince avatar Sep 19 '22 15:09 jlorince

can you please provide a spec to reproduce the issue?

wing328 avatar Sep 21 '22 07:09 wing328