Skip to content

[BUG][RUBY-NEXTGEN] enumUnknownDefaultCase: unknown values are not mapped, integer enums raise NameError #24996

Description

@wiebren

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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?
Description

With enumUnknownDefaultCase=true, ruby-nextgen has two problems:

  1. Unknown values are never mapped. The enum module declares UNKNOWN_DEFAULT_OPEN_API, but nothing maps an unknown value to it. Polymorphism.coerce_model calls const.build(data) when the enum module responds to build, and the enum module doesn't define it, so the raw value passes through unchanged.
  2. Integer enums fail to load. The unknown member is emitted in lower case (unknown_default_open_api = 11184809), so Ruby reads it as a local variable, not a constant, and Legs.all raises NameError. The numeric branch of RubyNextgenClientCodegen#toEnumVarName returns the name as is, without upcasing it.

Both are filed together because the fix for (1) calls valid? → all, so it only works for integer enums once (2) is fixed.

openapi-generator version

master, 7.26.0-SNAPSHOT, 05b61f34d7fb0199330e1d6c57e6159f72427837

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: enum
  version: 1.0.0
paths:
  /ponies/{id}:
    get:
      operationId: getPony
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pony'
components:
  schemas:
    Pony:
      type: object
      properties:
        type:
          $ref: '#/components/schemas/PonyType'
        legs:
          $ref: '#/components/schemas/Legs'
    PonyType:
      type: string
      enum: [Earth, Pegasus, Unicorn]
    Legs:
      type: integer
      enum: [2, 4]
Generation Details
java -jar openapi-generator-cli.jar generate -g ruby-nextgen -i spec.yaml -o out \
  --additional-properties=gemName=petstore,moduleName=Petstore,enumUnknownDefaultCase=true

Generated lib/petstore/models/legs.rb (pony_type.rb is the same shape, with UNKNOWN_DEFAULT_OPEN_API = 'unknown_default_open_api'):

    module Legs
      N2 = 2
      N4 = 4
      unknown_default_open_api = 11184809

      def self.all
        @all ||= [
          N2,
          N4,
          unknown_default_open_api,
        ].freeze
      end

      def self.valid?(value)
        all.include?(value)
      end
    end
Steps to reproduce
$LOAD_PATH.unshift 'out/lib'
require 'petstore'

p Petstore::Models::Pony.from_hash('type' => 'Kirin').type
p Petstore::Models::Legs.all

Actual (ruby 3.3):

"Kirin"
undefined local variable or method `unknown_default_open_api' for module Petstore::Models::Legs (NameError)

Expected:

"unknown_default_open_api"
[2, 4, 11184809]
Related issues/PRs
Suggest a fix

Give the enum module the build hook that Polymorphism.coerce_model already looks for:

--- a/modules/openapi-generator/src/main/resources/ruby-nextgen/partial_model_enum_class.mustache
+++ b/modules/openapi-generator/src/main/resources/ruby-nextgen/partial_model_enum_class.mustache
@@ -18,4 +18,11 @@
       def self.valid?(value)
         all.include?(value)
       end
+{{#enumUnknownDefaultCase}}
+
+      # A value this client does not know deserializes to the unknown default member.
+      def self.build(value)
+        valid?(value) ? value : {{#allowableValues}}{{#enumVars}}{{#-last}}{{{name}}}{{/-last}}{{/enumVars}}{{/allowableValues}}
+      end
+{{/enumUnknownDefaultCase}}
     end

and upcase the numeric branch:

--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyNextgenClientCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyNextgenClientCodegen.java
@@ -423,7 +423,8 @@ public class RubyNextgenClientCodegen extends AbstractRubyCodegen {
             String varName = name
                     .replace("-", "MINUS_")
                     .replace("+", "PLUS_")
-                    .replace(".", "_DOT_");
+                    .replace(".", "_DOT_")
+                    .toUpperCase(Locale.ROOT);
             return varName.matches("^\\d.*") ? NUMERIC_ENUM_PREFIX + varName : varName;
         }

With both changes the snippet prints the expected output.


Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions