Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 18, 2025

Extension properties containing multi-line strings (e.g., JSON examples with embedded triple-quotes) were converted using TypeSpec's triple-quoted string format, causing syntax errors when the content itself contained """.

Changes

  • Added escapedStringLiteral() function in common.ts that always uses single-line format with proper escaping of newlines, quotes, backslashes, and template literal syntax
  • Updated normalizeObjectValueToTSValueExpression() in decorators.ts to use escaped literals for all string values in object literals instead of auto-selecting triple-quoted format

Example

Given an OpenAPI extension with JSON content containing Python docstrings:

x-oaiMeta:
  example: |
    {
      "source": """
    def grade():
        """Returns 1.0"""
        return 1.0
    """,
    }

Before: Invalid syntax with nested triple-quotes

example: """
{
  "source": """
def grade():

After: Valid escaped string literal

example: "{\n  \"source\": \"\"\"\ndef grade():\n    \"\"\"Returns 1.0\"\"\"\n    return 1.0\n\"\"\",\n}\n"

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: /usr/local/bin/node node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/astro.js build sh nts/�� ebsite/src/content/docs/docs/emitters/openapi3/reference node /.bin/sh -p . ents/reference node js lmstxt --output-dir ../../websit--output-dir node (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>[Bug]: import - json in a string property is being interpreted as JSON</issue_title>
<issue_description>### Describe the bug

#9228 introduced a regression.

The following description leads to this invalid TypeSpec definition after being imported.

@extension("x-oaiMeta", #{name: "Python Grader", group: "graders", example: """
{
  "type": "python",
  "name": "Example python grader",
  "image_tag": "2025-05-08",
  "source": """
def grade(sample: dict, item: dict) -> float:
    \"""
    Returns 1.0 if `output_text` equals `label`, otherwise 0.0.
    \"""
    output = sample.get("output_text")
    label = item.get("label")
    return 1.0 if output == label else 0.0
""",
}

When in fact it should result in the following.

@extension(
  "x-oaiMeta",
  #{
    name: "Python Grader",
    group: "graders",
    example: "{\n  \"type\": \"python\",\n  \"name\": \"Example python grader\",\n  \"image_tag\": \"2025-05-08\",\n  \"source\": \"\"\"\ndef grade(sample: dict, item: dict) -> float:\n    \\\"\"\"\n    Returns 1.0 if `output_text` equals `label`, otherwise 0.0.\n    \\\"\"\"\n    output = sample.get(\"output_text\")\n    label = item.get(\"label\")\n    return 1.0 if output == label else 0.0\n\"\"\",\n}\n",
  }
)

Reproduction

components:
  schemas:
    GraderPython:
      x-oaiMeta:
        name: Python Grader
        group: graders
        example: |
          {
            "type": "python",
            "name": "Example python grader",
            "image_tag": "2025-05-08",
            "source": """
          def grade(sample: dict, item: dict) -> float:
              \"""
              Returns 1.0 if `output_text` equals `label`, otherwise 0.0.
              \"""
              output = sample.get("output_text")
              label = item.get("label")
              return 1.0 if output == label else 0.0
          """,
          }

Here the example property in the extension should be considered a literal string, and not JSON that needs to be parsed.

Checklist

<agent_instructions>use conventional commits.
before each commit, run the formatting command.
when you're done, run npx @chronus/chronus add to add a bugfix entry to the changelog.
start by defining the unit tests first.</agent_instructions>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix JSON interpretation issue in string property fix(openapi3): use escaped string literals in object values to prevent triple-quote syntax errors Dec 18, 2025
Copilot AI requested a review from baywet December 18, 2025 15:55
@baywet baywet marked this pull request as ready for review December 18, 2025 16:08
@baywet baywet enabled auto-merge December 18, 2025 16:08
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 18, 2025

Open in StackBlitz

npm i https://pkg.pr.new/microsoft/typespec/@typespec/openapi@9236
npm i https://pkg.pr.new/microsoft/typespec/@typespec/openapi3@9236

commit: 84952a8

@github-actions
Copy link
Contributor

github-actions bot commented Dec 19, 2025

All changed packages have been documented.

  • @typespec/openapi3
Show changes

@typespec/openapi3 - fix ✏️

Fix extension properties with JSON-like strings using escaped string literals to prevent triple-quote syntax issues

@baywet baywet added this pull request to the merge queue Dec 29, 2025
Merged via the queue into main with commit 6632212 Dec 29, 2025
23 checks passed
@baywet baywet deleted the copilot/fix-json-interpreter-issue branch December 29, 2025 21:27
github-merge-queue bot pushed a commit that referenced this pull request Dec 30, 2025
#9236 and #9228 introduced regressions where some syntax could end up
being escaped twice, causing compilation issues after import
This is an attempt to definitively fix all the escaping issues, not
letting copilot drive...

---------

Signed-off-by: Vincent Biret <[email protected]>
Co-authored-by: Mark Cowlishaw <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: import - json in a string property is being interpreted as JSON

4 participants