Repository navigation
Description
Issue Type
quicktype output (generated Ruby code that fails at load time).
Context (Environment, Version, Language)
Input Format: JSON Schema
Output Language: Ruby
CLI, npm, or app.quicktype.io: CLI, built from master
Version: 24.0.0 (master at c39af7d)
Description
Found while porting quicktype to Rust (quickertype). For a recursive
schema, the generated Ruby file declares a class that refers to a union
class (Types.Instance(XElement)) before that union class is declared,
so require-ing the file raises NameError. The repository's own input
test/inputs/schema/recursive-union-flattening.schema shows it: the
schema-ruby fixture fails on it. CI runs ruby,schema-ruby-syntax,
which only checks the syntax (ruby -c), so it does not show up there.
Input Data
test/inputs/schema/recursive-union-flattening.schema:
{
"items": {
"anyOf": [
{ "$ref": "#/definitions/a" },
{ "additionalProperties": { "$ref": "#" } }
]
},
"definitions": {
"a": {
"properties": {
"x": {
"items": { "$ref": "#/definitions/a" }
}
}
}
}
}Expected Behaviour / Output
The file loads, and TopLevel.from_json! round-trips
recursive-union-flattening.1.json.
Current Behaviour / Output
dry-core-1.0.0/lib/dry/core/deprecations.rb:227:in 'block (2 levels) in deprecate_constant': uninitialized constant X::XElement (NameError)
from top_level.rb:35:in '<class:X>'
from top_level.rb:29:in '<top (required)>'
The generated file declares, in this order:
# (forward declaration)
class XClass < Dry::Struct; end
class X < Dry::Struct
...
attribute :union_array, Types.Array(Types.Instance(XElement)).optional
...
end
class XElement < Dry::Struct
...Steps to Reproduce
node dist/index.js --src-lang schema --lang ruby --top-level TopLevel --src test/inputs/schema/recursive-union-flattening.schema -o top_level.rbruby -e "require './top_level.rb'"with thedry-typesanddry-structgems oftest/fixtures/ruby- Or:
QUICKTEST=true FIXTURE=schema-ruby npm run test:fixtures -- test/inputs/schema/recursive-union-flattening.schema
Possible Solution
X and XElement are unions, which RubyRenderer emits as Dry::Struct
classes (emitUnion). They are in a cycle with the class XClass, so
DeclarationIR forward-declares the types of the cycle that
canBeForwardDeclared accepts, and RubyRenderer accepts only classes:
protected canBeForwardDeclared(t: Type): boolean {
return t.kind === "class";
}A union that needs a name (unionNeedsName) is a class too and can be
forward-declared the same way (class XElement < Dry::Struct; end).
A note on the order: within the cycle, after the forward declarations,
declarationsForGraph builds the graph of the remaining types with
new Graph(rest, true, ...) (inverted), while the full graph uses
typeGraph.makeGraph(false, ...). Building it with false also puts
XElement before X here, but it changes the declaration order for every
language with forward declarations, so the fix I'd propose stays in the
Ruby renderer.
Written by Claude (an AI assistant made by Anthropic) and posted on behalf of the account owner (EloB). The reproduction, fix and tests were checked by running them as described above.
Activity