Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 5, 2025

  • Analyze the issue: OpenAPI 3.1/3.2 schemas with contentEncoding: base64 should be imported as bytes type with @encode("base64", string) decorator
  • Understand the codebase: The import/convert functionality is in packages/openapi3/src/cli/actions/convert/
  • Modify generate-types.ts (getStringType function) to handle contentEncoding: base64 and return bytes type
  • Modify decorators.ts (getStringSchemaDecorators function) to add @encode("base64", string) decorator when contentEncoding: base64
  • Add tests to verify the fix
  • Run format and lint commands
  • Add changelog entry
  • Run code review and address feedback
  • Manually verify the generated TypeSpec output is correct
  • Fix TypeScript build error in data-types.test.ts

Summary

This PR fixes the import of OpenAPI 3.1/3.2 schemas with contentEncoding: base64.

Before this fix:

# OpenAPI 3.1/3.2
properties:
  b64_json:
    type: string
    contentEncoding: base64

Was incorrectly imported as:

model Foo {
  b64_json?: string;  // Wrong!
}

After this fix:

model Foo {
  @encode("base64", string) b64_json?: bytes;  // Correct!
}

Security Summary

The changes are minimal and focused on type detection during import. No security vulnerabilities were introduced.

Original prompt

This section details on the original issue you should resolve

<issue_title>import - base64 content encoding should be imported as bytes</issue_title>
<issue_description>### Clear and concise description of the problem

The following TypeSpec definition

model Foo {
  @encode("base64", string)
  b64_json?: bytes;
}

Leads to the follow OpenAPI 3.1/3.2 descriptions

openapi: 3.2.0
info:
  title: (title)
  version: 0.0.0
tags: []
paths: {}
components:
  schemas:
    Foo:
      type: object
      properties:
        b64_json:
          type: string
          contentEncoding: base64

And the following OpenAPI 3.0 description

openapi: 3.0.0
info:
  title: (title)
  version: 0.0.0
tags: []
paths: {}
components:
  schemas:
    Foo:
      type: object
      properties:
        b64_json:
          type: string
          format: base64

But importing those to TypeSpec leads to the following result in 3.1/3.2

model Foo {
  b64_json?: string;
}

And this result in 3.0

model Foo {
  @format("base64") b64_json?: string;
}

The import result looks correct for OpenAPI 3.0 due to the lack of support for contentEncoding. However the import in 3.1/3.2 should lead to a bytes type and an encode decorator.

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 sure you run the format and lint commands before each commit.
use conventional commits.
run npx @chronus/chronus add to add a bugfix changelog entry when done with the changes.</agent_instructions>

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


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix base64 content encoding import as bytes fix: import OpenAPI 3.1/3.2 contentEncoding base64 as bytes with @encode decorator Dec 5, 2025
Copilot AI requested a review from baywet December 5, 2025 18:46
@baywet
Copy link
Member

baywet commented Dec 5, 2025

@copilot I'm getting the following error when building, fix it.

test/tsp-openapi3/data-types.test.ts(83,11): error TS2353: Object literal may only specify known properties, and 'contentEncoding' does not exist in type 'Refable<OpenAPI3Schema>'

Copy link
Contributor Author

Copilot AI commented Dec 5, 2025

@copilot I'm getting the following error when building, fix it.

...

Fixed by casting the test schema to any in commit 1ac932b. The contentEncoding property is only available in OpenAPI 3.1+ schemas (OpenAPISchema3_1), but the test utility uses OpenAPI3Schema type which is for OpenAPI 3.0. The cast allows the test to compile while still correctly testing the functionality.

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 sion�� ebsite/src/content/docs/docs/emitters/openapi3/reference node .0/bin/sh /library-linter node . rsioning/referengenerate-scenarios-summary node tool�� lmstxt --output-dir ../../website/src/content/docs/docs/libraries/http/reference node _modules/.bin/node /library-linter sh . st/node-gyp-bin/tsx ./.scripts/regen-compiler-docs.ts sh (dns block)

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

Copilot AI requested a review from baywet December 5, 2025 19:08
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 5, 2025

Open in StackBlitz

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

commit: 15b3a9b

@baywet baywet marked this pull request as ready for review December 5, 2025 19:27
@baywet baywet enabled auto-merge December 5, 2025 19:27
@baywet baywet requested a review from timotheeguerin December 8, 2025 13:33
@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2025

All changed packages have been documented.

  • @typespec/openapi3
Show changes

@typespec/openapi3 - fix ✏️

Import OpenAPI 3.1/3.2 schemas with contentEncoding: base64 as bytes type with @encode("base64", string) decorator

@baywet baywet added this pull request to the merge queue Dec 8, 2025
Merged via the queue into main with commit adf2cd5 Dec 8, 2025
23 checks passed
@baywet baywet deleted the copilot/fix-base64-encoding-import branch December 8, 2025 14:44
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.

import - base64 content encoding should be imported as bytes

3 participants