Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose a TS API for adding extension fields to a schema in a custom decorator #3336

Open
3 tasks done
Veetaha opened this issue May 13, 2024 · 1 comment
Open
3 tasks done
Assignees
Milestone

Comments

@Veetaha
Copy link
Contributor

Veetaha commented May 13, 2024

Clear and concise description of the problem

This issue is based on a discussion #3328 and @timotheeguerin's suggestion to solve my particular use case. See details in that discussion. The gist is that I'd like to write a custom decorator that simplifies the following notation:

@extension("x-foo", Json<"value here">)

to the this one

@foo("value here")

Right now it's implemented in my code with a hack like this, which creates a "fake" template instantiation of Json<T> model.

import { createRekeyableMap } from "@typespec/compiler/utils";
import { $extension } from "@typespec/json-schema";

/**
* @param {import("@typespec/compiler").DecoratorContext} context
* @param {import("@typespec/compiler").Type} target
* @param {import("@typespec/compiler").StringLiteral} value
*/
export function $foo(context, target, value) {
   $extension(context, target, "x-foo", {
       kind: "Model",
       name: "Json",
       namespace: { name: "JsonSchema" },
       properties: createRekeyableMap([["value", { type: value }]])
   })
}

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.
@Veetaha Veetaha changed the title Expose a JS API for adding extension fields to a schema in a custom decorator Expose a TS API for adding extension fields to a schema in a custom decorator May 13, 2024
@markcowl markcowl added this to the [2024] June milestone May 14, 2024
@markcowl
Copy link
Contributor

est: 3
pri: 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants