Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/quicktype-core/src/language/Ruby/RubyRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { mapContains } from "collection-utils";

import {
ConvenienceRenderer,
type ForbiddenWordsInfo,
Expand Down Expand Up @@ -570,6 +572,20 @@ export class RubyRenderer extends ConvenienceRenderer {
table.push([[name], [` = "${stringEscape(json)}"`]]);
});
this.emitTable(table);

if (this._options.justTypes || !mapContains(this.topLevels, e)) {
return;
}

this.ensureBlankLine();
this.emitBlock("def self.from_dynamic!(d)", () => {
this.emitLine(this.fromDynamic(e, "d"));
});

this.ensureBlankLine();
this.emitBlock("def self.from_json!(json)", () => {
this.emitLine("from_dynamic!(JSON.parse(json))");
});
});
}

Expand Down
2 changes: 0 additions & 2 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,6 @@ export const RubyLanguage: Language = {
],
skipSchema: [
"integer-before-number.schema", // Python-specific union-order regression.
// We don't generate a convenience method for top-level enums
"top-level-enum.schema",
// Top-level scalar arrays redefine Array#to_json recursively.
"issue2680-top-level-array.schema",
],
Expand Down
30 changes: 30 additions & 0 deletions test/unit/ruby-top-level-enum-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, test } from "vitest";

import { InputData, JSONSchemaInput, quicktype } from "quicktype-core";

async function rubyForSchema(schema: object): Promise<string> {
const schemaInput = new JSONSchemaInput(undefined);
await schemaInput.addSource({
name: "TopLevel",
schema: JSON.stringify(schema),
});

const inputData = new InputData();
inputData.addInput(schemaInput);

const result = await quicktype({ inputData, lang: "ruby" });
return result.lines.join("\n");
}

test("Ruby only generates enum parsing helpers for top-level enums", async () => {
const output = await rubyForSchema({
type: "object",
properties: {
status: { type: "string", enum: ["ready", "waiting"] },
},
required: ["status"],
});

expect(output.match(/def self\.from_dynamic!/g)).toHaveLength(1);
expect(output.match(/def self\.from_json!/g)).toHaveLength(1);
});
Loading