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

[BUG] [R] Initialize validation/check of an enum parameter

Open vaucouleur opened this issue 2 years ago • 2 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)?
  • [x] 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

Summary: Initialize validation/check of an enum parameter in the outer R6 class against an empty vector.

In the example below:

  • The outer component contains a kind property of type my_enum
  • The my_enum component has two enum cases x and y
  • The initialize function of outer checks kind with respect to the empty vector c(), hence this condition is always false:
  initialize = function(`kind`, ...) {
      if (!missing(`kind`)) {
        if (!(`kind` %in% c())) {
          stop(paste("Error! \"", `kind`, "\" cannot be assigned to `kind`. Must be .", sep = ""))
        }
        stopifnot(R6::is.R6(`kind`))
        self$`kind` <- `kind`
      }
    },
openapi-generator version

7.4.0-SNAPSHOT (via docker openapitools/openapi-generator-cli:latest)

OpenAPI declaration file content or url
{
  "openapi": "3.0.3",
  "info": {
    "title": "test",
    "version": "1.0.0"
  },
  "paths":{
  },
  "components": {
    "schemas": {
      "outer": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "kind"
        ],
        "properties": {
          "kind": {
            "$ref": "#/components/schemas/my_enum"
          }
        }
      },
      "my_enum": {
        "type": "string",
        "enum": [
          "x",
          "y"
        ]
      }
    }
  }
}
Generation Details
Steps to reproduce
docker run --user 1000:1000 --rm -v /tmp/test:/test openapitools/openapi-generator-cli:latest generate --global-property models -i /test/test.json -g r -o /test
Related issues/PRs

Possibly related to (but not sure):

  • https://github.com/OpenAPITools/openapi-generator/issues/13469
Suggest a fix

vaucouleur avatar Feb 27 '24 15:02 vaucouleur

Hi, from the first looks of it, shouldn`t your enum be defined as

"my_enum": {
  "type": "array",
  "enum": [
    "x",
    "y"
  ]
}

instead of having type "string"?

joXemMx avatar Mar 18 '24 15:03 joXemMx

@joXemMx

Looking at this, for example, we see, at the bottom of the page, that the enum Color is of type string:

components:
  schemas:
    Color:
      type: string
      enum:
        - black
        - white
        - red
        - green
        - blue

vaucouleur avatar Apr 17 '24 12:04 vaucouleur