OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.42k stars 6.48k forks source link

[BUG][R] model properties cannot use reserved words self, super, private, initialize as names #9776

Open ahjota opened 3 years ago

ahjota commented 3 years ago

Bug Report Checklist

Description

Generating the R client for a spec that has a model with property named one of self, private, or super creates code with runtime errors.

> devtools::load_all(".")
ℹ Loading openapi
Error in R6::R6Class("MyObject", public = list(public = NULL, private = NULL,  : 
  Items cannot use reserved names 'self', 'private', and 'super'. 
 11. stop("Items cannot use reserved names 'self', 'private', and 'super'.") 
 10. R6::R6Class("MyObject", public = list(public = NULL, private = NULL, 
         self = NULL, initialize = function(public = NULL, private = NULL, 
             self = NULL, ...) {
             local.optional.var <- list(...) ... at my_object.R#25
 9. eval(exprs[i], envir) 
 8. eval(exprs[i], envir) 
 7. source_one(file, encoding, envir = envir) 
 6. source_many(paths, encoding, env) 
 5. force(code) 
 4. withr_with_dir(path, source_many(paths, encoding, env)) 
 3. load_code(path) 
 2. pkgload::load_all(path = path, reset = reset, recompile = recompile, 
    export_all = export_all, helpers = helpers, quiet = quiet, 
    ...) 
 1. devtools::load_all(".") 

Additionally, using initialize will cause a different error:

Error in R6::R6Class("MyObject", public = list(initialize = NULL, initialize = function(initialize = NULL,  : 
  All items in public, private, and active must have unique names.

In the R6 OO system, self, private, and super are used to access public members / private members / superclass methods. initialize is the class initializer.

https://r6.r-lib.org/articles/Introduction.html#fields-containing-reference-objects-1

openapi-generator version

I am using openapi-generator 5.1.1.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: R reserved words bug example
  version: 0.0.1
paths:
  /example:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MyObject'
      responses:
        '200':
          description: OK
components:
  schemas:
   MyObject:
    properties:
      self:
        type: string
    type: object
Generation Details
openapi-generator generate -i spec.yaml -g r -o rtest
Steps to reproduce

Prereq: R and RStudio installed

  1. Open RStudio
  2. Open project, navigate to rtest output directory. This will create an .Rproj file in rtest.
  3. In the RStudio Console, run devtools::load_all() to load the library.
Related issues/PRs
Suggest a fix

I've created a gist with a generated model file that is broken as well as what I think the expected file should look like.

I'm guessing this means changes to all of the model* templates.

ahjota commented 3 years ago

I corrected this issue to reference the fact that super is a reserved keyword in the R6 OO system, not public. Additionally, initialize will also cause runtime issues as this word is reserved for the class initializer... new()-ing a model generated with the property initialize will throw the following at runtime:

Error in R6::R6Class("MyObject", public = list(initialize = NULL, initialize = function(initialize = NULL,  : 
  All items in public, private, and active must have unique names.