Skip to content

generate-schema: block-ref props lose the inline literal and the named saved-block options that deco (Fresh) offered #568

Open

Description

@aline-pereira

Summary

For a section prop typed as a loader output (e.g. posts: Post[]), generate-schema.ts emits

"posts": {
  "anyOf": [
    { "$ref": "#/definitions/Resolvable" },   // "Select from saved" — untyped
    { "$ref": "#/definitions/<loader-1>" },
    { "$ref": "#/definitions/<loader-2>" },
    … 
  ]
}

deco (Fresh) emitted, for the same prop, the inline literal type plus one named entry per saved block whose __resolveType produces that type — on top of the same loader list. Two editor-facing options are lost in the migration:

  1. The inline literal. Fresh's first anyOf branch is { type: "array", items: { $ref: "…@Post" } }, so an editor can hand-author the value in the admin instead of being forced through a loader. That branch has no equivalent here.
  2. Named saved blocks. Fresh listed each matching saved block by its own name. Here they collapse into the single generic Resolvable entry (title: "Select from saved", additionalProperties: true, __resolveType: { type: "string" }) — which carries no type information, so the admin can neither filter the picker to blocks that actually produce the right type nor show them by name.

Repro

Site: deco-sites/oficina-tanstack (migrated from deco-sites/oficina-reserva), @decocms/[email protected].

Section site/sections/home/home-the-post.tsx:

export interface Props {
  id?: string;
  posts: Post[];
}

deco / Fresh — https://www.oficina.com/live/_meta, definition …blog.ts@[Post], 8 options:

1. [Post]  (inline array — type: array, items: $ref …@Post)
2. site/loaders/sanity/get-auxiliary-category-posts.ts
3. site/loaders/sanity/get-older-posts-private.ts
4. site/loaders/sanity/get-older-posts.ts
5. site/loaders/sanity/get-posts.ts
6. [Private] Posts Antigos               ← saved block
7. the-post-hero-banner-loader           ← saved block
8. the-post-recommended-posts-loader     ← saved block

@decocms/blocks — .deco/meta.gen.json, …home-the-post.tsx@Props.properties.posts, 5 options:

1. Resolvable ("Select from saved")   ← replaces #1 and #6–8 above
2. site/loaders/sanity/get-auxiliary-category-posts.ts
3. site/loaders/sanity/get-older-posts-private.ts
4. site/loaders/sanity/get-older-posts.ts
5. site/loaders/sanity/get-posts.ts

All three saved blocks exist in this repo's .deco/blocks/ and all three resolve to site/loaders/sanity/get-posts.ts, so they are valid values for the prop — they just aren't offered by name any more.

Where

scripts/generate-schema.ts, the "Loader output type → block-ref" branch (~L620):

if (matchingLoaders?.length) {
  const blockRefSchema: any = {
    anyOf: [
      { $ref: `#/definitions/${RESOLVABLE_KEY}` },
      ...matchingLoaders.map((k) => ({ $ref: `#/definitions/${toBase64(k)}` })),
    ],
    title: name.charAt(0).toUpperCase() + name.slice(1),
  };

The loader keys come from ctx.outputTypeToLoaderKeys. There is no equivalent pass over .deco/blocks/*.json to expand saved blocks whose __resolveType is one of matchingLoaders, and no branch that emits the resolved literal schema for the prop's own type.

Impact

Not limited to this one prop: 160 props across this site's generated schema use the anyOf: [Resolvable, …loaders] shape, and 38 of 442 saved blocks have a loader __resolveType — i.e. every one of those blocks is currently reachable only through the untyped generic picker.

Concretely for editors:

  • No way to author a literal value inline where Fresh allowed it.
  • Named, reusable loader blocks stop appearing by name in the prop's picker, and the remaining Resolvable entry can't be type-filtered (__resolveType is just string, additionalProperties: true), so the picker either lists blocks that don't produce the right type or requires the editor to know the block name by heart.
  • Values already saved in the CMS keep working at runtime — this is an admin/authoring regression, not a rendering one.

Suggested direction

In the same branch, before building anyOf:

  1. Emit the prop's own resolved literal schema as the first branch (mirroring what the __SECTION_REF__/array path already does for sections), so hand-authoring stays possible.
  2. Expand matchingLoaders over the block source: for each saved block whose __resolveType ∈ matchingLoaders, emit a named definition (title = block name, __resolveType enum pinned to that block id), the way Fresh did.

Happy to send a PR if the direction is right — would prefer a steer on whether the saved-block expansion belongs in generate-schema.ts at build time or in the admin's picker at runtime, since .deco/blocks/ is content and meta.gen.json is otherwise code-derived.

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