Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 29, 2025

Fixes #8511 by exposing a public setOperationId function that allows other emitters to programmatically set operation IDs on operations.

Problem

Previously, emitters could only read operation IDs using getOperationId() but had no way to programmatically set them. The only way to set operation IDs was through the @operationId decorator, which couldn't be applied programmatically from JavaScript/TypeScript code. This made it impossible for custom emitters to modify operation IDs before passing them to the OpenAPI emitter.

Solution

Added a new setOperationId(program: Program, entity: Operation, opId: string): void function that:

  • Allows programmatic setting of operation IDs from any emitter
  • Uses the same internal state management as the @operationId decorator
  • Is properly exported from the OpenAPI library's public API
  • Integrates seamlessly with existing getOperationId() and resolveOperationId() functions

Usage

import { setOperationId, getOperationId } from "@typespec/openapi";

// In your custom emitter:
function customEmitter(program: Program) {
  for (const [name, operation] of program.getGlobalNamespaceType().operations) {
    if (!getOperationId(program, operation)) {
      // Set custom operation ID based on your logic
      setOperationId(program, operation, `custom_${operation.name}`);
    }
  }
}

The OpenAPI emitter will automatically pick up these programmatically set operation IDs when generating specifications.

Testing

Added comprehensive test coverage including:

  • Basic programmatic operation ID setting
  • Interaction with decorator-set operation IDs
  • Override scenarios in both directions
  • Integration with existing getOperationId() functionality

All existing tests continue to pass, ensuring no breaking changes.

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: node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/astro.js build (dns block)

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

Original prompt

This section details on the original issue you should resolve

<issue_title>OpenAPI emitter: expose setOperationId function to set and modify operations in other emitters</issue_title>
<issue_description>### Clear and concise description of the problem

I wanted to workaround this issue:

#8511

And figured that I can't that easily programmatically set operationId on an operation and pass modified program to openapi emitter.

I can't apply decorators from emitter (no js api for this).
And openapi emitter doesn't expose setOperationId function, only getOperationId.

So, I'd like to either have an openAPI exposed function to setOperationId on an operation, modifying the program.
Or have a way to programmatically set decorators using compiler API.

Checklist

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.</issue_description>

<agent_instructions>Make the setOperationId helper function a public function exported from the OpenApi library</agent_instructions>

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

Fixes #8576

💡 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.

Copilot AI changed the title [WIP] OpenAPI emitter: expose setOperationId function to set and modify operations in other emitters OpenAPI emitter: expose setOperationId function to set and modify operations in other emitters Sep 29, 2025
Copilot AI requested a review from markcowl September 29, 2025 19:06
Copilot finished work on behalf of markcowl September 29, 2025 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants