Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

[BUG][RUBY-NEXTGEN] Nothing autoloads when moduleName is not the camelized gemName

Đang mở Phù hợp với người mới
#24,997 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
70/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java, ruby

Hướng nghiên cứu

Lỗi nằm trong mẫu gem.mustache của trình tạo Ruby NextGen. Bắt đầu bằng cách định vị modules/openapi-generator/src/main/resources/ruby-nextgen/gem.mustache trong kho lưu trữ. Vấn đề là Zeitwerk::Loader.for_gem mong đợi tên gem khớp với tên mô-đun, nhưng khi chúng khác nhau, việc tự động tải sẽ thất bại. Cách sửa là luôn sử dụng bộ tải rõ ràng (Zeitwerk::Loader.new + push_dir) thay vì sử dụng có điều kiện. Kiểm tra bằng cách tạo một gem Ruby với gemName và moduleName không khớp, sau đó yêu cầu nó và kiểm tra xem các hằng số có được tải không. Diff được cung cấp cho thấy thay đổi chính xác cần thiết.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

When moduleName is not the camelized gemName (e.g. gemName=things_api, moduleName=Things), none of the generated constants load. For a single-level moduleName, gem.mustache sets Zeitwerk up with Zeitwerk::Loader.for_gem, which expects lib/things_api/** to define constants under ThingsApi. The generated files define them under Things, so Things::Client is never autoloaded and eager_load fails on ThingsApi.

gem.mustache already has a loader that works (Zeitwerk::Loader.new + push_dir(..., namespace: self)), but it only emits it when moduleName is nested.

openapi-generator version

master, 7.26.0-SNAPSHOT, 05b61f34d7fb0199330e1d6c57e6159f72427837

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: zeitwerk
  version: 1.0.0
paths:
  /things:
    get:
      operationId: listThings
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Thing'
components:
  schemas:
    Thing:
      type: object
      properties:
        name:
          type: string
Generation Details
java -jar openapi-generator-cli.jar generate -g ruby-nextgen -i spec.yaml -o out \
  --additional-properties=gemName=things_api,moduleName=Things

Generated lib/things_api.rb (comments trimmed):

module Things
  class << self
    attr_reader :loader
  end

  @loader = Zeitwerk::Loader.for_gem
  @loader.setup

  def self.eager_load!
    @loader.eager_load
  end
end
Steps to reproduce
$LOAD_PATH.unshift 'out/lib'
require 'things_api'

Things::Client.new(base_url: 'http://localhost')

Actual (ruby 3.3, zeitwerk 2.8.3):

uninitialized constant Things::Client (NameError)

Things.eager_load! raises uninitialized constant ThingsApi (NameError).

Expected: the client loads, as it does with gemName=things.

Related issues/PRs

None found.

Suggest a fix

Always use the explicit loader. push_dir(namespace: self) doesn't depend on gemName matching moduleName. With this change the snippet works for both gemName=things_api and gemName=things, and Things::VERSION still resolves.

--- a/modules/openapi-generator/src/main/resources/ruby-nextgen/gem.mustache
+++ b/modules/openapi-generator/src/main/resources/ruby-nextgen/gem.mustache
@@ -21,10 +21,9 @@
     attr_reader :loader
   end
 
-{{#moduleNameNested}}
-  # Nested moduleName: for_gem can't target a nested namespace, so drive Zeitwerk
-  # explicitly. The parent module(s) above are pre-defined so `module {{moduleName}}`
-  # (here and in every generated file) reopens rather than raising NameError.
+  # for_gem derives the namespace from gemName, which need not match moduleName (and
+  # can't target a nested one), so drive Zeitwerk explicitly. Any parent module(s) above
+  # are pre-defined so `module {{moduleName}}` reopens rather than raising NameError.
   @loader = Zeitwerk::Loader.new
   @loader.push_dir("#{__dir__}/{{gemName}}", namespace: self)
   # version.rb defines VERSION (not a `Version` class). for_gem's GemInflector special-cases
@@ -32,10 +31,6 @@
   # {{moduleName}}::VERSION available.
   @loader.ignore("#{__dir__}/{{gemName}}/version.rb")
   require_relative "{{gemName}}/version"
-{{/moduleNameNested}}
-{{^moduleNameNested}}
-  @loader = Zeitwerk::Loader.for_gem
-{{/moduleNameNested}}
 {{#zeitwerkInflections.0}}
   # Model names keep their acronym casing (e.g. HTTPConfig), but Zeitwerk's default
   # inflector would expect HttpConfig from the file name. Register the exceptions so

Generated with Claude Code

Ngôn ngữ chính
Java
Star
26.8k
Fork
7.7k
Merge trung bình
2 ngày 18 giờ
Pull request đã merge (30 ngày)
103

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của OpenAPITools/openapi-generator

Tất cả issue của OpenAPITools/openapi-generator

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.