Skip to content

fix: resolve default values from emitter options JSON schema#9971

Draft
Copilot wants to merge 9 commits intomainfrom
copilot/fix-emitter-options-defaults
Draft

fix: resolve default values from emitter options JSON schema#9971
Copilot wants to merge 9 commits intomainfrom
copilot/fix-emitter-options-defaults

Conversation

Copy link
Contributor

Copilot AI commented Mar 9, 2026

Minimal fix — only 4 files changed, no breaking changes to existing tests.

Changes Made

  • schema-validator.ts: Add useDefaults option to JSONSchemaValidatorOptions interface and pass it through to AJV (+2 lines)
  • library.ts: Enable useDefaults: true when creating the emitter option validator (+1 line)
  • emitter-options.test.ts: Add 3 test cases for schema defaults — applies defaults when no options provided, user values override defaults, and partial defaults for missing options (+76 lines)
  • Changelog entry: Documents the fix for @typespec/compiler

How It Works

The fix enables AJV's built-in useDefaults option specifically for the emitter option validator. When an emitter defines default values in its JSON schema options, those defaults are now applied to context.options during $onEmit. This is a non-breaking, additive change — existing emitters without default values in their schemas are unaffected.

Testing

  • ✅ All 8 emitter options tests pass (1 pre-existing skip)
  • ✅ No changes to any existing tests required
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: EmitterOptions defaults are not resolved.</issue_title>
<issue_description>### Describe the bug

Following the same example of custom configs definitions from here: https://typespec.io/docs/extending-typespec/emitters-basics/#custom-configuration-options, but using default for some property, doesn't in runtime doesn't result in default value being passed into emitter, so the typing is off.

Reproduction

E.g. having this from documentation:

export interface EmitterOptions {
  "target-name": string;
}

const EmitterOptionsSchema: JSONSchemaType<EmitterOptions> = {
  type: "object",
  additionalProperties: false,
  properties: {
    "target-name": { type: "string", nullable: true, default: null },
  },
  required: [],
};

In emitter

export const $lib = createTypeSpecLibrary({
  internal: internalLib,
  emitter: {
    options: EmitterOptionsSchema,
  },
});

export async function $onEmit(context: EmitContext<EmitterOptions>) {
  console.log(contenxt.options); // --> {}
}

If I'm providing json-schema, I'd expect defaults set in it to be used. Otherwise it doesn't make a lot of sense and usage of json-schema in this case should be documented on the terms of scopes it's being applied for. R.n. seems like only for IDE autocomplete integration for emitters, but no types validation nor defaults substitution.

Checklist

<agent_instructions>Find any issues in the backlog that address this deficiency</agent_instructions>

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

@markcowl @markcowl see if any duplicate issues

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Loading
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:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp emitter:client:java Issue for the Java client emitter: @typespec/http-client-java emitter:openapi3 Issues for @typespec/openapi3 emitter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: EmitterOptions defaults are not resolved.

2 participants