-
Notifications
You must be signed in to change notification settings - Fork 238
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
Updates extension decorator to support value kinds and adds setExtension API to json-schema emitter #3558
Updates extension decorator to support value kinds and adds setExtension API to json-schema emitter #3558
Conversation
All changed packages have been documented.
Show changes
|
You can try these changes at https://cadlplayground.z22.web.core.windows.net/prs/3558/ Check the website changes at https://tspwebsitepr.z22.web.core.windows.net/prs/3558/ |
…wn instead of any
50fbb9a
to
be60574
Compare
You can try these changes here
|
Related to #3336
Summary
This PR introduces 2 changes:
setExtension
API on the@typespec/json-schema
emitter to make it easier to author custom extension decorators.@extension
decorator to support taking values to obviate the need for theJson<Data>
model.Example usage of writing a custom extension
@added
decorator:Example usage of passing in value literals to
@extension
decorator:Background
The
@typespec/json-schema
package exports an@extension
decorator that allows customers to specify additional properties to be present as part of their emitted JSON schema.For example, we can add a property - converted to a schema - that indicates when a field was added to the model:
The decorator also supports emitting the property's value as raw code by wrapping the parameter in the
Json<Data>
template:Comparison
Below is a diff between the two ways of calling the decorator for clarity:
Pain Points
Currently,
@extension
only supports raw values by wrapping them with theTypeSpec.JsonSchema.Json
template. Since this type isn't available in JS code, this forces authors to manually create the wrapped type themselves, e.g.:Update
@extension
to accept values directly and exposesetExtension
APIThis PR exposes a new
setExtension
API that accepts types and values as input, instead of justType
. This also meansExtensionRecord.value
has been loosened:In addition, the
@extension
decorator is updated to support accepting values directly:While this change introduces breaking changes to the
@extensions
decorator andgetExtensions
function, it also allows the@extension
decorator to support value kinds as input - a feature language feature introduced in0.57.0
.This would not cause additional breaking changes (beyond
getExtensions
) for users that already use theTypeSpec.JsonSchema.Json
template to pass in raw values. However, there is 1 scenario that would now fail:(e.g.
@extension("x-bool-literal", true)
would need to be updated to@extension("x-bool-literal", typeof true)
to preserve current behavior)Alternatives
Solution - only expose
setExtension
APIThis PR exposes a new
setExtension
API that accepts types and values as input, instead of justType
. This also meansExtensionRecord.value
has been loosened:This does introduce a breaking change in the getExtensions API since
ExtensionRecord.value
is no longer guaranteed to beType
. However, it is likely this API is not used much if at all outside of this emitter.Create new
setExtensionValue
/getExtensionValues
functions and alternative@extension
decoratorThis change has the benefit of not requiring any breaking changes. However, we would now have 2 different but very similar APIs for settings/getting extensions that may lead to confusion, as well as 2 very similar extensions to do essentially the same thing.