Flatten nested unions in `Schema.toJsonSchemaDocument`
メンテナーはふだん 1 日以内に返信
関連するプルリクエストがすでにマージされています。
- #9549 @effect-bot による — マージ済み
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 74/100
- issue の種類
- 機能追加
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- json, typescript
- 領域
- backend
調査の方向性
Read packages/effect/src/internal/schema/toJsonSchemaDocument.ts, especially compileJsonSchema's Union case around the linked lines 611–623, and compare it with the existing nested allOf handling in appendJsonSchema around lines 187–190. Add coverage for nested anyOf output, including members with annotations or $refs and the stated oneOf limitation. Done when eligible nested anyOf members are flattened while other members retain their structure.
索引モデルが issue の本文から書いたものです。
説明
What is the problem this feature would solve?
The current output is valid; this is about its shape. Schema.toJsonSchemaDocument writes each member of a Union as one anyOf option, whatever that member is. NullOr of a union, or a union of unions, then comes out as an anyOf inside an anyOf:
import { Schema } from 'effect';
const Person = Schema.Struct({ name: Schema.String });
const Company = Schema.Struct({ company: Schema.String });
const Team = Schema.Struct({ team: Schema.String });
const owner = Schema.NullOr(Schema.Union([Person, Company]));
const member = Schema.Union([Schema.Union([Person, Company]), Team]);
console.log(JSON.stringify(Schema.toJsonSchemaDocument(owner).schema));
console.log(JSON.stringify(Schema.toJsonSchemaDocument(member).schema));
Output (abridged):
{"anyOf":[{"anyOf":[{…name…},{…company…}]},{"type":"null"}]}
{"anyOf":[{"anyOf":[{…name…},{…company…}]},{…team…}]}
The nested form means the same as the flat one, but it's harder to read in rendered docs, and client generators turn it into nested union types.
What is the feature you are proposing to solve the problem?
Write the flat form:
{"anyOf":[{…name…},{…company…},{"type":"null"}]}
{"anyOf":[{…name…},{…company…},{…team…}]}
In the Union case of compileJsonSchema, an anyOf member whose only keyword is anyOf can be replaced by its own members. A member carrying anything else, like an annotation or a $ref to a definition, has to stay as it is:
appendJsonSchema already splices a nested allOf this way:
The oneOf mode can't be flattened like this: a value matching every option of { oneOf: [{ oneOf: [a, b] }, c] } is accepted, while { oneOf: [a, b, c] } rejects it.
What alternatives have you considered?
Flattening the output afterwards, in each tool that consumes it. That's being proposed for an OpenAPI converter too, but output used directly, and every other consumer, would stay nested. The same nesting comes from other libraries' JSON Schema output:
- standard-community/standard-openapi#41
- open-circle/valibot#1710
- colinhacks/zod#6673
Version: effect 4.0.5
- 主要言語
- TypeScript
- スター
- 16.7k
- フォーク
- 808
- 平均マージ
- 10時間 12分
- マージ済み PR(30日)
- 813
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Effect-TS/effect のほかの issue
-
BrowserWorkerRunner: port finalizer throws when the worker global has no close() (Bun)対応中かも @santiago-ramos-02 が 20 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Effect-TS/effect#8635 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
Support {self: this} for fnUntraced再び着手できるかも @ArjunCodess が 31 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンenhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
Effect-TS/effect#8101 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 32/100
Effect-TS/effect#9610 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Schema.isStringFinite (and Number string matching) takes quadratic time on long digit runs対応中かも @magoz が今日担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 66/100
メンテナーはふだん 1 日以内に返信
-
Schema.isBase64 / isBase64Url throw RangeError on multi-megabyte strings対応中かも @magoz が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 20/100
メンテナーはふだん 1 日以内に返信
Effect-TS/effect の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
diegosouzapw/OmniRoute#17386 ·
メンテナーはふだん 1 日以内に返信
-
ready-for-agent
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
メンテナーはふだん 1 日以内に返信
-
area/dashboard kind/bug kind/bug-qa QA/dev-automation
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
vybestack/llxprt-code#3917 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
api-parity
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
IBM/carbon-components-ember#1157 ·
メンテナーはふだん 1 日以内に返信