Skip to content

fix(compiler): avoid ICE in serializeValueAsJson for no-argument scalar constructors#9804

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-ice-serialize-value-json
Draft

fix(compiler): avoid ICE in serializeValueAsJson for no-argument scalar constructors#9804
Copilot wants to merge 3 commits intomainfrom
copilot/fix-ice-serialize-value-json

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

serializeScalarValueAsJson would crash with an ICE when a custom scalar value used a no-argument initializer (e.g. S.i()), because the code unconditionally accessed value.value.args[0] which is undefined in that case.

Changes

  • packages/compiler/src/lib/examples.ts: When resolveKnownScalar returns undefined (scalar is not a known built-in), only return undefined if args[0] is missing (the no-arg case that caused the ICE). When args[0] is present, the original behavior of using it as the serialized value is preserved — this is intentional for cast-like scalar constructors (e.g. shortName.name("Shorty") correctly serializes as "Shorty").

  • packages/compiler/test/decorators/examples.test.ts: Added regression tests — the no-argument initializer case (test.i()) returns undefined, and the with-argument initializer case (test.name("Shorty")) serializes the first argument.

  • packages/openapi3/test/models.test.ts: Added a test verifying that a model property default using a no-argument scalar initializer (the exact repro from the issue) does not crash and produces no default value in the emitted schema.

Reproduction

scalar S {
  init i();
}

model M { p: S = S.i(); }  // Previously: ICE in serializeValueAsJson, now: default omitted
Original prompt

This section details on the original issue you should resolve

<issue_title>[core] ICE in serializeValueAsJson</issue_title>
<issue_description>### Describe the bug

The core library throws an uncaught Error on a custom scalar initializer with no arguments in serializeValueAsJson. The Error is provoked by the OpenAPI3 emitter (as well as the JSON Schema emitter) and bubbles back up to the emitter entrypoint where it triggers an ICE.

The logic attempts to use the first argument of the initializer, which isn't reliable. That logic works for "cast-like" scalar values (e.g. uuid("...")), but not for initializers (e.g. uuid.random() // no first argument!).

Reproduction

scalar S {
  init i();
}

model M { p: S = S.i(); }

https://typespec.io/playground/?e=%40typespec%2Fopenapi3&c=c2NhbGFyIFMgewogIGluaXQgaSgpOwp9Cgptb2RlbCBNIHsgcDogUyA9IFMuxBsgfQ%3D%3D&options=%7B%7D&vs=%7B%7D

However, if you provide a first argument, this is enough to quell the ICE but the emit is still wrong:

scalar S {
  init i(s: string);
}

// Default value renders as "foo", which is wrong.
model M { p: S = S.i("foo") }

https://typespec.io/playground/?e=%40typespec%2Fopenapi3&c=c2NhbGFyIFMgewogIGluaXQgaShzOiBzdHJpbmcpOwp9Cgptb2RlbCBNIHsgcDogUyA9IFMuaSgiZm9vIik7IH0%3D&options=%7B%7D&vs=%7B%7D

Checklist

<agent_instructions>Instead we should drop the result if we don't recognize the constructor</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@timotheeguerin Drop the result like we do with `now()` if we can't resolve the scalar constructor

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…n serializeValueAsJson

Co-authored-by: markcowl <1054056+markcowl@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ICE in serializeValueAsJson for custom scalars fix(compiler): avoid ICE in serializeValueAsJson for unrecognized scalar constructors Feb 25, 2026
@microsoft-github-policy-service microsoft-github-policy-service bot added the compiler:core Issues for @typespec/compiler label Feb 25, 2026
@markcowl
Copy link
Contributor

@copilot This fix is overbroad, causing 3 tests that use known scalars to fail. Please ensure that all tests are passing. Also, please add a test that checks the openapi3 serialization of a default value for a model property, similar to the one shown in the repro.

…reserve args[0] behavior

Co-authored-by: markcowl <1054056+markcowl@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 26, 2026

@copilot This fix is overbroad, causing 3 tests that use known scalars to fail. Please ensure that all tests are passing. Also, please add a test that checks the openapi3 serializat...

Fixed in e2f42d9. The previous fix was too broad — it dropped undefined for all unrecognized scalar constructors, breaking the existing shortName.name("Shorty") → "Shorty" behavior.

The correct fix only returns undefined when args[0] is missing (the no-arg case that caused the ICE), and preserves the original args[0] serialization for scalars with arguments.

Also added the openapi3 test that checks scalar S { init i(); } / model M { p: S = S.i(); } doesn't crash and produces no default value.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • telemetry.astro.build
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/astro.js build sh s/.b�� tspd --enable-experimental gen-e--llmstxt sh pec/node_modules/.bin/sh ental ture n-schema/referengenerate-scenarios-summary sh -c astro check --minimumFailingSeverity hint &amp;&amp; ast--filter node node_modules/.bin/tspd . sh (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:openapi3 Issues for @typespec/openapi3 emitter label Feb 26, 2026
Copilot AI changed the title fix(compiler): avoid ICE in serializeValueAsJson for unrecognized scalar constructors fix(compiler): avoid ICE in serializeValueAsJson for no-argument scalar constructors Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:core Issues for @typespec/compiler emitter:openapi3 Issues for @typespec/openapi3 emitter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[core] ICE in serializeValueAsJson

2 participants