Skip to content

[Bug]: @typespec/json-schema crash on OmitProperties-derived model #9465

@urbanautomaton

Description

@urbanautomaton

Describe the bug

I've run into the following crash when emitting an OmitProperties-derived subset of a model as @typespec/json-schema.

I think this occurs when one or more retained properties is a non-core type; I've illustrated in my reproduction with a derived string, but the same failure occurs for other derived scalars, as well as model types.

The source model Thing is successfully emitted on its own; it's only when the derived type with OmitProperties is introduced that the crash occurs.

If there's any further detail I can add, I'd be very happy to provide it, just let me know.

Reproduction

Playground link

// repro.tsp
import "@typespec/json-schema";

using JsonSchema;

scalar derived_string extends string;

model Thing {
  a: string;

  // fails:
  b: derived_string;

  // compiles:
  // b: string;
}

@jsonSchema
model SubsetThing extends OmitProperties<Thing, "a"> {}

Expected output

$schema: https://json-schema.org/draft/2020-12/schema
$id: SubsetThing.yaml
type: object
properties:
  b:
    $ref: "#/$defs/derived_string"
required:
  - b
$defs:
  derived_string:
    type: string

Actual output

$ npx tsp compile repro.tsp 
TypeSpec compiler v1.8.0

✔ Compiling
× Running @typespec/json-schema...
Emitter "@typespec/json-schema" crashed! This is a bug.
Please file an issue at https://github.com/microsoft/typespec/issues

TypeError: Cannot read properties of undefined (reading 'bundledRefs')
    at JsonSchemaEmitter.reference (file:///[snip]/node_modules/@typespec/json-schema/src/json-schema-emitter.ts:467:38)
    at invokeReference (file:///[snip]/node_modules/@typespec/json-schema/node_modules/@typespec/asset-emitter/src/asset-emitter.ts:289:31)
    at file:///[snip]/node_modules/@typespec/json-schema/node_modules/@typespec/asset-emitter/src/asset-emitter.ts:263:18
    at withPatchedReferenceContext (file:///[snip]/node_modules/@typespec/json-schema/node_modules/@typespec/asset-emitter/src/asset-emitter.ts:735:14)
    at Object.emitTypeReference (file:///[snip]/node_modules/@typespec/json-schema/node_modules/@typespec/asset-emitter/src/asset-emitter.ts:211:14)
    at JsonSchemaEmitter.modelPropertyLiteral (file:///[snip]/node_modules/@typespec/json-schema/src/json-schema-emitter.ts:301:39)
    at file:///[snip]/node_modules/@typespec/json-schema/node_modules/@typespec/asset-emitter/src/asset-emitter.ts:513:58
    at withTypeContext (file:///[snip]/node_modules/@typespec/json-schema/node_modules/@typespec/asset-emitter/src/asset-emitter.ts:709:5)
    at invokeTypeEmitter (file:///[snip]/node_modules/@typespec/json-schema/node_modules/@typespec/asset-emitter/src/asset-emitter.ts:501:5)
    at Object.emitModelProperty (file:///[snip]/node_modules/@typespec/json-schema/node_modules/@typespec/asset-emitter/src/asset-emitter.ts:439:14)

--------------------------------------------------
Library Version                1.8.0
TypeSpec Compiler Version      1.8.0
--------------------------------------------------

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions