Skip to content

[Bug]: json-schema emitter fails on @extension value referencing an enum #6695

@copland

Description

@copland

Describe the bug

When using an enum to define appropriate values for a given @extension, @typespec/json-schema fails with TypeError: Converting circular structure to JSON.

This example is sufficient to trigger the issue (same content as playground link):

import "@typespec/json-schema";

using JsonSchema;

@jsonSchema
namespace Schemas;

const MetadataTag = "x-metadata";

enum MetadataValue {
  Foo: "foo",
  Bar: "bar"
}

@extension(MetadataTag, MetadataValue.Foo)
model Car {
  /** Kind of car */
  kind: "ev" | "ice";

  /** Brand of the car */
  brand: string;

  /** Model of the car */
  `model`: string;
}

Running this locally you see:

> tsp compile .

TypeSpec compiler v0.67.2

✔ 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: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'id' -> object with constructor 'Object'
    --- property 'parent' closes the circle
    at JSON.stringify (<anonymous>)
    at #serializeSourceFileContent (file:///..path../node_modules/@typespec/json-schema/dist/src/json-schema-emitter.js:564:25)
    at JsonSchemaEmitter.sourceFile (file:///..path../node_modules/@typespec/json-schema/dist/src/json-schema-emitter.js:534:55)
    at Object.emitSourceFile (file:///..path../node_modules/@typespec/asset-emitter/dist/src/asset-emitter.js:347:38)
    at JsonSchemaEmitter.writeOutput (file:///..path../node_modules/@typespec/json-schema/dist/src/json-schema-emitter.js:517:54)
    at async Object.$onEmit [as emitFunction] (file:///..path../node_modules/@typespec/json-schema/dist/src/on-emit.js:19:5)
    at async runEmitter (file:///..path../node_modules/@typespec/compiler/dist/src/core/program.js:668:9)
    at async file:///..path../node_modules/@typespec/compiler/dist/src/core/program.js:647:9
    at async trackAction (file:///..path../node_modules/@typespec/compiler/dist/src/core/logger/console-sink.js:121:24)
    at async emit (file:///..path../node_modules/@typespec/compiler/dist/src/core/program.js:646:5)

--------------------------------------------------
Library Version                0.67.1
TypeSpec Compiler Version      0.67.2
--------------------------------------------------

It is possible to work around this by changing the enum to a set of const values:

const MetadataTag = "x-metadata";

const Foo = "foo";
const Bar = "bar";

@extension(MetadataTag, Foo)
model Car { ... }

Reproduction

Playground Link

NOTE: The playground link fails a bit differently (with a call stack error rather than a type error)

Checklist

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions