OpenAPITools / OpenAPITools/openapi-generator

[BUG][RUBY] Integer default enum value references constant that does not exist

Open
#17,218 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

Generating code for ruby with an integer enum with a default references a constant that does not exist for the default value. See example

openapi-generator version

openapi-generator-cli 7.2.0-SNAPSHOT commit : 46dd875 (docker)

OpenAPI declaration file content or url
{
  "openapi": "3.0.3",
  "info": {
    "title": "Sample API",
    "version": "1.0.0"
  },
  "paths": {},
  "components": {
    "schemas": {
      "ExampleObject": {
        "type": "object",
        "properties": {
          "state": {
            "type": "integer",
            "enum": [
              128,
              2
            ],
            "default": 128,
            "description": "The state of the object."
} } } } } }
Generation Details
❯ mkdir config
❯ touch config/dummy.json
# paste the definition into dummy.json
❯ docker run --rm \
                                             -v "$(PWD)/config/:/local" \
                                             -v "$(PWD):/out" \
                                            --pull always \
                                             openapitools/openapi-generator-cli generate \
                                               --skip-validate-spec \
                                               --generator-name ruby --output /out \
                                               --input-spec /local/dummy.json \
                                               --additional-properties gemName=dummy

Steps to reproduce

In the generated code, search for STATE::N128. There is no declaration for this constant. The N128 seems to come from the default 128. Example:

# lib/probely/models/example_object.rb

      if attributes.key?(:'state') 
        self.state = attributes[:'state']
      else
        self.state = STATE::N1 # Line 86
      end

Related issues/PRs

This looks very similar to https://github.com/OpenAPITools/openapi-generator/issues/16455 but for Ruby. I have tested with the latest version and it's still happening for Ruby

Suggest a fix

If the property is a string, it references the value directly, rather than a constant:

      if attributes.key?(:'state')
        self.state = attributes[:'state']
      else
        self.state = 'active'
      end

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 the generated lib/probely/models/example_object.rb shown in the report and reproduce the issue using the provided OpenAPI JSON and Docker command. Trace how the Ruby generator handles an integer enum default compared with a string default. Done means the generated default refers to a declared value or otherwise produces valid Ruby without an undefined STATE::N128 constant.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, ruby
Domain
backend-api-design, 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.