Skip to content

Commit 6295e8e

Browse files
authored
Add an optional input/output json schema for handlers (#499)
1 parent 423190a commit 6295e8e

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

packages/restate-sdk/src/types/components.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ export class ServiceComponent implements Component {
6464
required: false,
6565
contentType:
6666
serviceHandler.handlerWrapper.accept ?? "application/json",
67+
jsonSchema: serviceHandler.handlerWrapper.inputSchema,
6768
},
6869
output: {
6970
setContentTypeIfEmpty: false,
7071
contentType:
7172
serviceHandler.handlerWrapper.contentType ?? "application/json",
73+
jsonSchema: serviceHandler.handlerWrapper.outputSchema,
7274
},
7375
documentation: serviceHandler.handlerWrapper.description,
7476
metadata: serviceHandler.handlerWrapper.metadata,
@@ -151,10 +153,12 @@ export class VirtualObjectComponent implements Component {
151153
input: {
152154
required: false,
153155
contentType: opts.accept ?? "application/json",
156+
jsonSchema: opts.inputSchema,
154157
},
155158
output: {
156159
setContentTypeIfEmpty: false,
157160
contentType: opts.contentType ?? "application/json",
161+
jsonSchema: opts.outputSchema,
158162
},
159163
ty:
160164
opts.kind === HandlerKind.EXCLUSIVE
@@ -235,10 +239,12 @@ export class WorkflowComponent implements Component {
235239
input: {
236240
required: false,
237241
contentType: handler.accept ?? "application/json",
242+
jsonSchema: handler.inputSchema,
238243
},
239244
output: {
240245
setContentTypeIfEmpty: false,
241246
contentType: handler.contentType ?? "application/json",
247+
jsonSchema: handler.outputSchema,
242248
},
243249
ty:
244250
handler.kind === HandlerKind.WORKFLOW

packages/restate-sdk/src/types/rpc.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ export type ServiceHandlerOpts = {
284284
* Additional metadata for the handler.
285285
*/
286286
metadata?: Record<string, string>;
287+
288+
/**
289+
* An optional JSON schema definition for the input parameter.
290+
* Please note that this is not enforced, but only used as a documentation hint.
291+
*/
292+
inputSchema?: object;
293+
294+
/**
295+
* An optional JSON schema definition for the input parameter.
296+
* Please note that this is not enforced, but only used as a documentation hint.
297+
*/
298+
outputSchema?: object;
287299
};
288300

289301
export type ObjectHandlerOpts = {
@@ -325,6 +337,18 @@ export type ObjectHandlerOpts = {
325337
* Additional metadata for the handler.
326338
*/
327339
metadata?: Record<string, string>;
340+
341+
/**
342+
* An optional JSON schema definition for the input parameter.
343+
* Please note that this is not enforced, but only used as a documentation hint.
344+
*/
345+
inputSchema?: object;
346+
347+
/**
348+
* An optional JSON schema definition for the input parameter.
349+
* Please note that this is not enforced, but only used as a documentation hint.
350+
*/
351+
outputSchema?: object;
328352
};
329353

330354
export type WorkflowHandlerOpts = {
@@ -366,6 +390,18 @@ export type WorkflowHandlerOpts = {
366390
* Additional metadata for the handler.
367391
*/
368392
metadata?: Record<string, string>;
393+
394+
/**
395+
* An optional JSON schema definition for the input parameter.
396+
* Please note that this is not enforced, but only used as a documentation hint.
397+
*/
398+
inputSchema?: object;
399+
400+
/**
401+
* An optional JSON schema definition for the input parameter.
402+
* Please note that this is not enforced, but only used as a documentation hint.
403+
*/
404+
outputSchema?: object;
369405
};
370406

371407
const HANDLER_SYMBOL = Symbol("Handler");
@@ -393,7 +429,9 @@ export class HandlerWrapper {
393429
outputSerde,
394430
opts?.accept,
395431
opts?.description,
396-
opts?.metadata
432+
opts?.metadata,
433+
opts?.inputSchema,
434+
opts?.outputSchema
397435
);
398436
}
399437

@@ -412,7 +450,9 @@ export class HandlerWrapper {
412450
public readonly outputSerde: Serde<unknown>,
413451
accept?: string,
414452
public readonly description?: string,
415-
public readonly metadata?: Record<string, string>
453+
public readonly metadata?: Record<string, string>,
454+
public readonly inputSchema?: object,
455+
public readonly outputSchema?: object
416456
) {
417457
this.accept = accept ? accept : inputSerde.contentType;
418458
this.contentType = outputSerde.contentType;

0 commit comments

Comments
 (0)