@scalar/mock-server 0.21.4 → 0.21.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @scalar/mock-server
2
2
 
3
+ ## 0.21.6
4
+
5
+ ### Patch Changes
6
+
7
+ - [#11105](https://github.com/scalar/scalar/pull/11105): Finish migrating legacy `nullable` schema keywords when upgrading OpenAPI 3.0 descriptions, so downstream request validators can compile the upgraded schemas and enforce the rest of the request contract.
8
+
9
+ ## 0.21.5
10
+
11
+ ### Patch Changes
12
+
13
+ - [#11029](https://github.com/scalar/scalar/pull/11029): fix: respect contains match counts in simple array examples
14
+
15
+ Generate matching and nonmatching primitive items for direct contains constraints, counting tuple prefixes and respecting item schemas, length bounds, uniqueness, and the declared dialect. Authored examples remain authoritative. Candidate generation is bounded and may return a partial example for contradictory constraints.
16
+
17
+ Complex references and compositions, nested object or array candidate synthesis, and XML branch reconstruction retain existing best-effort behavior and are deferred to a follow-up.
18
+ - [#11099](https://github.com/scalar/scalar/pull/11099): Update compatible runtime and styling dependencies, align editor dependency versions, and preserve document-change comparisons with updated microdiff semantics.
19
+ - [#11108](https://github.com/scalar/scalar/pull/11108): Keep validating requests when a schema `pattern` is only valid without the unicode flag.
20
+ A pattern such as `^\d{5}(\-\d{4})?$` that uses an escape like `\-`, `\_` or `\ ` now compiles as a plain ECMA-262 regular expression, so the body or the parameters of that operation are no longer left unchecked, and a pattern that parses with the unicode flag keeps it.
21
+ - [#11029](https://github.com/scalar/scalar/pull/11029): Preserve authored array item examples when contains constraints cannot be satisfied, and pass only dialect context to streaming example generation.
22
+
3
23
  ## 0.21.4
4
24
 
5
25
  ### Patch Changes
@@ -9,5 +9,5 @@ export const createMockServer = async (configuration) => {
9
9
  const processed = await processDocument(configuration?.document ?? configuration?.specification, configuration?.origin);
10
10
  return processed.type === 'asyncapi'
11
11
  ? createAsyncApiMockServer(configuration, processed.document)
12
- : createOpenApiMockServer(configuration, processed.document);
12
+ : createOpenApiMockServer(configuration, processed.document, processed.originalOpenapiVersion);
13
13
  };
@@ -6,5 +6,5 @@ import type { ProcessedDocument } from './utils/process-document.js';
6
6
  */
7
7
  export declare const createOpenApiMockServer: (configuration: MockServerOptions, schema: Extract<ProcessedDocument, {
8
8
  type: "openapi";
9
- }>["document"]) => Promise<Hono>;
9
+ }>["document"], originalOpenapiVersion?: string) => Promise<Hono>;
10
10
  //# sourceMappingURL=create-openapi-mock-server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-openapi-mock-server.d.ts","sourceRoot":"","sources":["../src/create-openapi-mock-server.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgB,IAAI,EAA0B,MAAM,MAAM,CAAA;AAIjE,OAAO,KAAK,EAAE,iBAAiB,EAA0B,MAAM,SAAS,CAAA;AAexE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAgFjE;;GAEG;AACH,eAAO,MAAM,uBAAuB,GAClC,eAAe,iBAAiB,EAChC,QAAQ,OAAO,CAAC,iBAAiB,EAAE;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC,CAAC,UAAU,CAAC,KAClE,OAAO,CAAC,IAAI,CAsPd,CAAA"}
1
+ {"version":3,"file":"create-openapi-mock-server.d.ts","sourceRoot":"","sources":["../src/create-openapi-mock-server.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgB,IAAI,EAA0B,MAAM,MAAM,CAAA;AAIjE,OAAO,KAAK,EAAE,iBAAiB,EAA0B,MAAM,SAAS,CAAA;AAexE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAgFjE;;GAEG;AACH,eAAO,MAAM,uBAAuB,GAClC,eAAe,iBAAiB,EAChC,QAAQ,OAAO,CAAC,iBAAiB,EAAE;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC,CAAC,UAAU,CAAC,EACnE,yBAAyB,MAAM,KAC9B,OAAO,CAAC,IAAI,CA2Pd,CAAA"}
@@ -82,8 +82,13 @@ const carriesResponse = (error) => 'getResponse' in error && typeof error.getRes
82
82
  /**
83
83
  * Create a mock server instance
84
84
  */
85
- export const createOpenApiMockServer = async (configuration, schema) => {
85
+ export const createOpenApiMockServer = async (configuration, schema, originalOpenapiVersion) => {
86
86
  const app = new Hono();
87
+ const generationContext = {
88
+ openapiVersion: schema.openapi,
89
+ jsonSchemaDialect: schema.jsonSchemaDialect,
90
+ originalOpenapiVersion,
91
+ };
87
92
  // Unhandled errors would otherwise reach Hono's default handler, which answers with a plain-text
88
93
  // `Internal Server Error` that says nothing about what broke. A mock server fails for mundane,
89
94
  // document-shaped reasons — a response header name the runtime rejects, an example that cannot be
@@ -249,10 +254,10 @@ export const createOpenApiMockServer = async (configuration, schema) => {
249
254
  const hasHandler = handlerCode && typeof handlerCode === 'string' && handlerCode.trim().length > 0;
250
255
  // Route to appropriate handler
251
256
  if (hasHandler) {
252
- handlers.push(async (c) => await mockHandlerResponse(c, operation, pathItem?.parameters, schema.openapi));
257
+ handlers.push(async (c) => await mockHandlerResponse(c, operation, pathItem?.parameters, generationContext));
253
258
  }
254
259
  else {
255
- handlers.push(async (c) => await mockAnyResponse(c, operation, schema.openapi));
260
+ handlers.push(async (c) => await mockAnyResponse(c, operation, generationContext));
256
261
  }
257
262
  // The pinned query parameters are not part of the route, so they are checked here. A request
258
263
  // that does not carry them is handed on to the next matching route — usually the sibling path
@@ -1,7 +1,8 @@
1
1
  import type { OpenAPIV3_1 } from '@scalar/openapi-types';
2
2
  import type { Context } from 'hono';
3
+ import { type ExampleGenerationContext } from '../utils/generate-response-example.js';
3
4
  /**
4
5
  * Mock any response
5
6
  */
6
- export declare function mockAnyResponse(c: Context, operation: OpenAPIV3_1.OperationObject, openapiVersion?: string): Response;
7
+ export declare function mockAnyResponse(c: Context, operation: OpenAPIV3_1.OperationObject, generationContext?: ExampleGenerationContext): Response;
7
8
  //# sourceMappingURL=mock-any-response.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mock-any-response.d.ts","sourceRoot":"","sources":["../../src/routes/mock-any-response.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAKxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AA8BnC;;GAEG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,WAAW,CAAC,eAAe,EAAE,cAAc,CAAC,EAAE,MAAM,YAuK1G"}
1
+ {"version":3,"file":"mock-any-response.d.ts","sourceRoot":"","sources":["../../src/routes/mock-any-response.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAKxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAMnC,OAAO,EAAE,KAAK,wBAAwB,EAA2B,MAAM,mCAAmC,CAAA;AAwB1G;;GAEG;AACH,wBAAgB,eAAe,CAC7B,CAAC,EAAE,OAAO,EACV,SAAS,EAAE,WAAW,CAAC,eAAe,EACtC,iBAAiB,GAAE,wBAA6B,YA2KjD"}
@@ -30,7 +30,7 @@ const IGNORED_RESPONSE_HEADERS = new Set([
30
30
  /**
31
31
  * Mock any response
32
32
  */
33
- export function mockAnyResponse(c, operation, openapiVersion) {
33
+ export function mockAnyResponse(c, operation, generationContext = {}) {
34
34
  // Note: the `onRequest` callback runs as middleware (see `create-mock-server`) so it also fires
35
35
  // for requests rejected before reaching this handler.
36
36
  // Parse the Prefer header (RFC 7240) so clients can request a specific
@@ -59,7 +59,9 @@ export function mockAnyResponse(c, operation, openapiVersion) {
59
59
  }
60
60
  const headerObject = getResolvedRef(headers[header]);
61
61
  // Headers use the generator defaults to preserve their declared values.
62
- const value = headerObject?.schema ? getExampleFromSchema(getResolvedRefDeep(headerObject.schema)) : null;
62
+ const value = headerObject?.schema
63
+ ? getExampleFromSchema(getResolvedRefDeep(headerObject.schema), generationContext)
64
+ : null;
63
65
  // Loose check on purpose: Hono *deletes* a header when handed `undefined`. This loop is the first
64
66
  // thing to set the declared headers, so in practice a delete removes a header set earlier by
65
67
  // middleware — `cors()` sets `Access-Control-Allow-Origin` before the handler runs.
@@ -93,6 +95,7 @@ export function mockAnyResponse(c, operation, openapiVersion) {
93
95
  c.header('Content-Type', acceptedContentType);
94
96
  const acceptedResponse = selectedResponse?.content?.[acceptedContentType];
95
97
  const streamingResponse = getStreamingResponse(acceptedResponse, acceptedContentType, {
98
+ ...generationContext,
96
99
  exampleName: prefer.example,
97
100
  variables: pathParameters(c),
98
101
  });
@@ -102,7 +105,7 @@ export function mockAnyResponse(c, operation, openapiVersion) {
102
105
  }
103
106
  const responseSchema = acceptedResponse?.schema ? getResolvedRefDeep(acceptedResponse.schema) : undefined;
104
107
  /** Generates the response body from the schema, or returns `undefined` when there is no schema. */
105
- const generateFromSchema = () => responseSchema ? generateResponseExample(responseSchema, pathParameters(c)) : undefined;
108
+ const generateFromSchema = () => responseSchema ? generateResponseExample(responseSchema, pathParameters(c), generationContext) : undefined;
106
109
  // Server-Sent Events are a framed, multi-event wire format, so they cannot go out as one buffered
107
110
  // body: a client reading the stream expects `data:` lines terminated by a blank line. Everything
108
111
  // else (JSON, XML, text) keeps taking the single-body path below.
@@ -131,7 +134,7 @@ export function mockAnyResponse(c, operation, openapiVersion) {
131
134
  c.status(statusCode);
132
135
  if (isXmlMediaType(acceptedContentType)) {
133
136
  const result = getXmlBodyExample(acceptedResponse?.schema, selectedExample, {
134
- openapiVersion,
137
+ ...generationContext,
135
138
  emptyString: 'string',
136
139
  variables: pathParameters(c),
137
140
  mode: 'read',
@@ -1,8 +1,9 @@
1
1
  import type { OpenAPIV3_1 } from '@scalar/openapi-types';
2
2
  import type { Context } from 'hono';
3
+ import { type ExampleGenerationContext } from '../utils/generate-response-example.js';
3
4
  /**
4
5
  * Mock response using x-handler code.
5
6
  * Executes the handler and returns its result as the response.
6
7
  */
7
- export declare function mockHandlerResponse(c: Context, operation: OpenAPIV3_1.OperationObject, pathItemParameters?: OpenAPIV3_1.PathItemObject['parameters'], openapiVersion?: string): Promise<Response>;
8
+ export declare function mockHandlerResponse(c: Context, operation: OpenAPIV3_1.OperationObject, pathItemParameters?: OpenAPIV3_1.PathItemObject['parameters'], generationContext?: ExampleGenerationContext): Promise<Response>;
8
9
  //# sourceMappingURL=mock-handler-response.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mock-handler-response.d.ts","sourceRoot":"","sources":["../../src/routes/mock-handler-response.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAIxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAqJnC;;;GAGG;AACH,wBAAsB,mBAAmB,CACvC,CAAC,EAAE,OAAO,EACV,SAAS,EAAE,WAAW,CAAC,eAAe,EACtC,kBAAkB,CAAC,EAAE,WAAW,CAAC,cAAc,CAAC,YAAY,CAAC,EAC7D,cAAc,CAAC,EAAE,MAAM,qBA8ExB"}
1
+ {"version":3,"file":"mock-handler-response.d.ts","sourceRoot":"","sources":["../../src/routes/mock-handler-response.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAIxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAKnC,OAAO,EAAE,KAAK,wBAAwB,EAA2B,MAAM,mCAAmC,CAAA;AAmJ1G;;;GAGG;AACH,wBAAsB,mBAAmB,CACvC,CAAC,EAAE,OAAO,EACV,SAAS,EAAE,WAAW,CAAC,eAAe,EACtC,kBAAkB,CAAC,EAAE,WAAW,CAAC,cAAc,CAAC,YAAY,CAAC,EAC7D,iBAAiB,GAAE,wBAA6B,qBA+EjD"}
@@ -19,7 +19,7 @@ import { getStreamingResponse, sendStreamingResponse } from '../utils/streaming-
19
19
  * `examples` map; otherwise it falls back to the singular `example`, the
20
20
  * first entry of the map, or a value generated from the schema.
21
21
  */
22
- function getExampleFromResponse(c, statusCode, responses, exampleName, openapiVersion) {
22
+ function getExampleFromResponse(c, statusCode, responses, exampleName, generationContext = {}) {
23
23
  if (!responses) {
24
24
  return null;
25
25
  }
@@ -45,7 +45,7 @@ function getExampleFromResponse(c, statusCode, responses, exampleName, openapiVe
45
45
  if (isXmlMediaType(acceptedContentType)) {
46
46
  c.header('Content-Type', acceptedContentType);
47
47
  const result = getXmlBodyExample(acceptedResponse.schema, selectedExample, {
48
- openapiVersion,
48
+ ...generationContext,
49
49
  emptyString: 'string',
50
50
  variables: pathParameters(c),
51
51
  mode: 'read',
@@ -66,7 +66,7 @@ function getExampleFromResponse(c, statusCode, responses, exampleName, openapiVe
66
66
  value = normalizeResponseBody(selectedExample.value, responseSchema);
67
67
  }
68
68
  else if (responseSchema) {
69
- value = normalizeResponseBody(generateResponseExample(responseSchema, pathParameters(c)), responseSchema);
69
+ value = normalizeResponseBody(generateResponseExample(responseSchema, pathParameters(c), generationContext), responseSchema);
70
70
  }
71
71
  // Legacy examples retain the handler fallback's JSON encoding policy.
72
72
  return JSON.stringify(value) ?? null;
@@ -124,7 +124,7 @@ function determineStatusCode(tracking) {
124
124
  * Mock response using x-handler code.
125
125
  * Executes the handler and returns its result as the response.
126
126
  */
127
- export async function mockHandlerResponse(c, operation, pathItemParameters, openapiVersion) {
127
+ export async function mockHandlerResponse(c, operation, pathItemParameters, generationContext = {}) {
128
128
  // Note: the `onRequest` callback runs as middleware (see `create-mock-server`) so it also fires
129
129
  // for requests rejected before reaching this handler.
130
130
  // Get x-handler code from operation
@@ -135,7 +135,7 @@ export async function mockHandlerResponse(c, operation, pathItemParameters, open
135
135
  }
136
136
  try {
137
137
  // Build handler context with tracking
138
- const { context, tracking } = await buildHandlerContext(c, operation, pathItemParameters);
138
+ const { context, tracking } = await buildHandlerContext(c, operation, pathItemParameters, generationContext);
139
139
  // Execute handler
140
140
  const { result } = await executeHandler(handlerCode, context);
141
141
  // Determine status code based on all store operations, prioritizing semantically meaningful ones
@@ -149,6 +149,7 @@ export async function mockHandlerResponse(c, operation, pathItemParameters, open
149
149
  const response = getResolvedRef(operation.responses?.[String(statusCode)] ?? operation.responses?.default);
150
150
  const contentType = negotiateContentType(c, response?.content);
151
151
  const streamingResponse = getStreamingResponse(response?.content?.[contentType], contentType, {
152
+ ...generationContext,
152
153
  body: result ?? undefined,
153
154
  exampleName: parsePreferHeader(c.req.header('Prefer')).example,
154
155
  variables: pathParameters(c),
@@ -163,7 +164,7 @@ export async function mockHandlerResponse(c, operation, pathItemParameters, open
163
164
  if (result === undefined || result === null) {
164
165
  // Try to pick up example response from OpenAPI spec if available
165
166
  const prefer = parsePreferHeader(c.req.header('Prefer'));
166
- const exampleResponse = getExampleFromResponse(c, statusCode, operation.responses, prefer.example, openapiVersion);
167
+ const exampleResponse = getExampleFromResponse(c, statusCode, operation.responses, prefer.example, generationContext);
167
168
  if (exampleResponse !== null) {
168
169
  return c.body(exampleResponse);
169
170
  }
@@ -1,5 +1,6 @@
1
1
  import type { OpenAPIV3_1 } from '@scalar/openapi-types';
2
2
  import type { Context } from 'hono';
3
+ import { type ExampleGenerationContext } from './generate-response-example.js';
3
4
  import { type StoreOperationTracking, createStoreWrapper } from './store-wrapper.js';
4
5
  /**
5
6
  * Context object provided to x-handler code.
@@ -25,6 +26,6 @@ type HandlerContextResult = {
25
26
  /**
26
27
  * Build the handler context from a Hono context.
27
28
  */
28
- export declare function buildHandlerContext(c: Context, operation?: OpenAPIV3_1.OperationObject, pathItemParameters?: OpenAPIV3_1.PathItemObject['parameters']): Promise<HandlerContextResult>;
29
+ export declare function buildHandlerContext(c: Context, operation?: OpenAPIV3_1.OperationObject, pathItemParameters?: OpenAPIV3_1.PathItemObject['parameters'], generationContext?: ExampleGenerationContext): Promise<HandlerContextResult>;
29
30
  export {};
30
31
  //# sourceMappingURL=build-handler-context.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-handler-context.d.ts","sourceRoot":"","sources":["../../src/utils/build-handler-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAGxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AASnC,OAAO,EAAE,KAAK,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAGjF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC,cAAc,CAAC,CAAA;IAC5D,GAAG,EAAE;QACH,IAAI,EAAE,GAAG,CAAA;QACT,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QAC9B,oFAAoF;QACpF,KAAK,EAAE,OAAO,CAAA;QACd,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAChC,CAAA;IACD,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CACzB,CAAA;AAED;;GAEG;AACH,KAAK,oBAAoB,GAAG;IAC1B,OAAO,EAAE,cAAc,CAAA;IACvB,QAAQ,EAAE,sBAAsB,CAAA;CACjC,CAAA;AA8DD;;GAEG;AACH,wBAAsB,mBAAmB,CACvC,CAAC,EAAE,OAAO,EACV,SAAS,CAAC,EAAE,WAAW,CAAC,eAAe,EACvC,kBAAkB,CAAC,EAAE,WAAW,CAAC,cAAc,CAAC,YAAY,CAAC,GAC5D,OAAO,CAAC,oBAAoB,CAAC,CAuD/B"}
1
+ {"version":3,"file":"build-handler-context.d.ts","sourceRoot":"","sources":["../../src/utils/build-handler-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAGxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAInC,OAAO,EAAE,KAAK,wBAAwB,EAA2B,MAAM,6BAA6B,CAAA;AAKpG,OAAO,EAAE,KAAK,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAGjF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC,cAAc,CAAC,CAAA;IAC5D,GAAG,EAAE;QACH,IAAI,EAAE,GAAG,CAAA;QACT,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QAC9B,oFAAoF;QACpF,KAAK,EAAE,OAAO,CAAA;QACd,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAChC,CAAA;IACD,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CACzB,CAAA;AAED;;GAEG;AACH,KAAK,oBAAoB,GAAG;IAC1B,OAAO,EAAE,cAAc,CAAA;IACvB,QAAQ,EAAE,sBAAsB,CAAA;CACjC,CAAA;AAmED;;GAEG;AACH,wBAAsB,mBAAmB,CACvC,CAAC,EAAE,OAAO,EACV,SAAS,CAAC,EAAE,WAAW,CAAC,eAAe,EACvC,kBAAkB,CAAC,EAAE,WAAW,CAAC,cAAc,CAAC,YAAY,CAAC,EAC7D,iBAAiB,GAAE,wBAA6B,GAC/C,OAAO,CAAC,oBAAoB,CAAC,CAuD/B"}
@@ -13,7 +13,7 @@ import { getStreamingResponse } from './streaming-response.js';
13
13
  * Get example response from OpenAPI spec for a given status code.
14
14
  * Returns the example value if found, or null if not available.
15
15
  */
16
- function getExampleFromResponse(c, statusCode, responses) {
16
+ function getExampleFromResponse(c, statusCode, responses, generationContext = {}) {
17
17
  if (!responses) {
18
18
  return null;
19
19
  }
@@ -39,6 +39,7 @@ function getExampleFromResponse(c, statusCode, responses) {
39
39
  return null;
40
40
  }
41
41
  const streamingResponse = getStreamingResponse(acceptedResponse, acceptedContentType, {
42
+ ...generationContext,
42
43
  exampleName: parsePreferHeader(c.req.header('Prefer')).example,
43
44
  variables: pathParameters(c),
44
45
  });
@@ -51,13 +52,13 @@ function getExampleFromResponse(c, statusCode, responses) {
51
52
  return normalizeResponseBody(acceptedResponse.example, responseSchema);
52
53
  }
53
54
  return responseSchema
54
- ? normalizeResponseBody(generateResponseExample(responseSchema, pathParameters(c)), responseSchema)
55
+ ? normalizeResponseBody(generateResponseExample(responseSchema, pathParameters(c), generationContext), responseSchema)
55
56
  : null;
56
57
  }
57
58
  /**
58
59
  * Build the handler context from a Hono context.
59
60
  */
60
- export async function buildHandlerContext(c, operation, pathItemParameters) {
61
+ export async function buildHandlerContext(c, operation, pathItemParameters, generationContext = {}) {
61
62
  let body = undefined;
62
63
  try {
63
64
  // Compare case-insensitively, since media types are case-insensitive and request validation
@@ -95,7 +96,7 @@ export async function buildHandlerContext(c, operation, pathItemParameters) {
95
96
  const res = {};
96
97
  if (operation?.responses) {
97
98
  for (const statusCode of Object.keys(operation.responses)) {
98
- res[statusCode] = getExampleFromResponse(c, statusCode, operation.responses);
99
+ res[statusCode] = getExampleFromResponse(c, statusCode, operation.responses, generationContext);
99
100
  }
100
101
  }
101
102
  return {
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Compile a schema `pattern` for Ajv, with the unicode flag when the pattern allows it and without it otherwise.
3
+ *
4
+ * OpenAPI reads `pattern` as an ECMA-262 regular expression, where escapes such as `\-` outside a class, `\_`
5
+ * or `\ ` are valid. Ajv compiles every pattern with the `u` flag, which turns those escapes into syntax
6
+ * errors, so a single zip code or phone number pattern made the whole body, or every parameter of its
7
+ * location, fail open. A pattern that parses with `u` keeps it, so `\p{L}` and astral characters still match
8
+ * the way they did, and a pattern that parses in neither mode still throws as before.
9
+ */
10
+ export declare const compilePattern: ((pattern: string, flags: string) => RegExp) & {
11
+ code: string;
12
+ };
13
+ //# sourceMappingURL=compile-pattern.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-pattern.d.ts","sourceRoot":"","sources":["../../src/utils/compile-pattern.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,aACf,MAAM,SAAS,MAAM,KAAG,MAAM;;CASlB,CAAA"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Compile a schema `pattern` for Ajv, with the unicode flag when the pattern allows it and without it otherwise.
3
+ *
4
+ * OpenAPI reads `pattern` as an ECMA-262 regular expression, where escapes such as `\-` outside a class, `\_`
5
+ * or `\ ` are valid. Ajv compiles every pattern with the `u` flag, which turns those escapes into syntax
6
+ * errors, so a single zip code or phone number pattern made the whole body, or every parameter of its
7
+ * location, fail open. A pattern that parses with `u` keeps it, so `\p{L}` and astral characters still match
8
+ * the way they did, and a pattern that parses in neither mode still throws as before.
9
+ */
10
+ export const compilePattern = Object.assign((pattern, flags) => {
11
+ try {
12
+ return new RegExp(pattern, flags);
13
+ }
14
+ catch {
15
+ return new RegExp(pattern, flags.replace('u', ''));
16
+ }
17
+ },
18
+ // Only read when Ajv generates standalone code, which the mock server never does.
19
+ { code: 'compilePattern' });
@@ -1,9 +1,11 @@
1
1
  import { getExampleFromSchema } from '@scalar/workspace-store/request-example';
2
2
  /** The schema shape `getExampleFromSchema` accepts, so callers do not have to name it themselves. */
3
3
  export type ExampleSchema = Parameters<typeof getExampleFromSchema>[0];
4
+ /** The source dialect travels together so normalized and original versions cannot be swapped. */
5
+ export type ExampleGenerationContext = Pick<NonNullable<Parameters<typeof getExampleFromSchema>[1]>, 'openapiVersion' | 'jsonSchemaDialect' | 'originalOpenapiVersion'>;
4
6
  /**
5
7
  * Generate response bodies and message payloads with response-mode defaults.
6
8
  * Headers use the generator directly to preserve empty strings and write-only values.
7
9
  */
8
- export declare const generateResponseExample: (schema: ExampleSchema, variables?: Record<string, unknown>) => unknown;
10
+ export declare const generateResponseExample: (schema: ExampleSchema, variables?: Record<string, unknown>, generationContext?: ExampleGenerationContext) => unknown;
9
11
  //# sourceMappingURL=generate-response-example.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"generate-response-example.d.ts","sourceRoot":"","sources":["../../src/utils/generate-response-example.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,yCAAyC,CAAA;AAE9E,qGAAqG;AACrG,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAA;AAEtE;;;GAGG;AACH,eAAO,MAAM,uBAAuB,GAAI,QAAQ,aAAa,EAAE,YAAY,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,OAKjG,CAAA"}
1
+ {"version":3,"file":"generate-response-example.d.ts","sourceRoot":"","sources":["../../src/utils/generate-response-example.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,yCAAyC,CAAA;AAE9E,qGAAqG;AACrG,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAA;AAEtE,iGAAiG;AACjG,MAAM,MAAM,wBAAwB,GAAG,IAAI,CACzC,WAAW,CAAC,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,EACvD,gBAAgB,GAAG,mBAAmB,GAAG,wBAAwB,CAClE,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,uBAAuB,GAClC,QAAQ,aAAa,EACrB,YAAY,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,oBAAmB,wBAA6B,KAC/C,OAMC,CAAA"}
@@ -3,7 +3,8 @@ import { getExampleFromSchema } from '@scalar/workspace-store/request-example';
3
3
  * Generate response bodies and message payloads with response-mode defaults.
4
4
  * Headers use the generator directly to preserve empty strings and write-only values.
5
5
  */
6
- export const generateResponseExample = (schema, variables) => getExampleFromSchema(schema, {
6
+ export const generateResponseExample = (schema, variables, generationContext = {}) => getExampleFromSchema(schema, {
7
+ ...generationContext,
7
8
  emptyString: 'string',
8
9
  variables,
9
10
  mode: 'read',
@@ -4,6 +4,7 @@ import type { AsyncApiDocument } from '@scalar/types/asyncapi/3.1';
4
4
  export type ProcessedDocument = {
5
5
  type: 'openapi';
6
6
  document: OpenAPIV3_1.Document | OpenAPIV3_2.Document;
7
+ originalOpenapiVersion?: string;
7
8
  } | {
8
9
  type: 'asyncapi';
9
10
  document: AsyncApiDocument;
@@ -1 +1 @@
1
- {"version":3,"file":"process-document.d.ts","sourceRoot":"","sources":["../../src/utils/process-document.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAErE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAOlE,8FAA8F;AAC9F,MAAM,MAAM,iBAAiB,GACzB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,EAAE,WAAW,CAAC,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAA;CAAE,GAC1E;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,EAAE,gBAAgB,CAAA;CAAE,CAAA;AAEpD;;;;GAIG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAU,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EACtD,SAAS,MAAM,KACd,OAAO,CAAC,iBAAiB,CAmF3B,CAAA"}
1
+ {"version":3,"file":"process-document.d.ts","sourceRoot":"","sources":["../../src/utils/process-document.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAErE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAOlE,8FAA8F;AAC9F,MAAM,MAAM,iBAAiB,GACzB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,EAAE,WAAW,CAAC,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC;IAAC,sBAAsB,CAAC,EAAE,MAAM,CAAA;CAAE,GAC3G;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,EAAE,gBAAgB,CAAA;CAAE,CAAA;AAEpD;;;;GAIG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAU,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EACtD,SAAS,MAAM,KACd,OAAO,CAAC,iBAAiB,CA6F3B,CAAA"}
@@ -73,6 +73,11 @@ export const processDocument = async (document, origin) => {
73
73
  // Upgrading must not activate a $self field authored in an older OpenAPI version.
74
74
  const retrievalUri = origin ?? (typeof document === 'string' && (isFilePath(document) || isHttpUrl(document)) ? document : '/');
75
75
  const documentUri = resolveOpenApiDocument(bundled, retrievalUri)?.baseUri;
76
+ const originalOpenapiVersion = typeof bundled.openapi === 'string'
77
+ ? bundled.openapi
78
+ : typeof bundled.swagger === 'string'
79
+ ? bundled.swagger
80
+ : undefined;
76
81
  let upgraded;
77
82
  try {
78
83
  upgraded = upgrade(bundled, '3.2', { onIncompatible: 'collect' }).document;
@@ -85,5 +90,9 @@ export const processDocument = async (document, origin) => {
85
90
  }
86
91
  // Wrap the document in a magic proxy so internal references resolve lazily via `$ref-value`.
87
92
  // External references were already pulled inline by `bundle` above, so only local `$ref`s remain.
88
- return { type: 'openapi', document: createMagicProxy(upgraded, { documentUri }) };
93
+ return {
94
+ type: 'openapi',
95
+ document: createMagicProxy(upgraded, { documentUri }),
96
+ originalOpenapiVersion,
97
+ };
89
98
  };
@@ -1,5 +1,6 @@
1
1
  import type { OpenAPIV3_2 } from '@scalar/openapi-types';
2
2
  import type { Context } from 'hono';
3
+ import { type ExampleGenerationContext } from './generate-response-example.js';
3
4
  /** A finite mock stream, with its data-model value available to custom handlers. */
4
5
  type StreamingResponse = {
5
6
  body: unknown;
@@ -13,7 +14,7 @@ type StreamingResponse = {
13
14
  */
14
15
  export declare const getStreamingResponse: (mediaType: (Omit<OpenAPIV3_2.MediaTypeObject, "itemSchema"> & {
15
16
  itemSchema?: OpenAPIV3_2.MediaTypeObject["itemSchema"] | boolean;
16
- }) | undefined, contentType: string, options?: {
17
+ }) | undefined, contentType: string, options?: ExampleGenerationContext & {
17
18
  exampleName?: string;
18
19
  variables?: Record<string, unknown>;
19
20
  body?: unknown;
@@ -1 +1 @@
1
- {"version":3,"file":"streaming-response.d.ts","sourceRoot":"","sources":["../../src/utils/streaming-response.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAKxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAMnC,oFAAoF;AACpF,KAAK,iBAAiB,GAAG;IACvB,IAAI,EAAE,OAAO,CAAA;IACb,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,GAC/B,WACI,CAAC,IAAI,CAAC,WAAW,CAAC,eAAe,EAAE,YAAY,CAAC,GAAG;IACjD,UAAU,CAAC,EAAE,WAAW,CAAC,eAAe,CAAC,YAAY,CAAC,GAAG,OAAO,CAAA;CACjE,CAAC,GACF,SAAS,EACb,aAAa,MAAM,EACnB,UAAS;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAAC,IAAI,CAAC,EAAE,OAAO,CAAA;CAAO,KAC1F,iBAAiB,GAAG,SA8CtB,CAAA;AAED,oGAAoG;AACpG,eAAO,MAAM,qBAAqB,GAAI,GAAG,OAAO,EAAE,UAAU,iBAAiB,KAAG,QAY/E,CAAA"}
1
+ {"version":3,"file":"streaming-response.d.ts","sourceRoot":"","sources":["../../src/utils/streaming-response.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAKxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAGnC,OAAO,EAAE,KAAK,wBAAwB,EAA2B,MAAM,6BAA6B,CAAA;AAGpG,oFAAoF;AACpF,KAAK,iBAAiB,GAAG;IACvB,IAAI,EAAE,OAAO,CAAA;IACb,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,GAC/B,WACI,CAAC,IAAI,CAAC,WAAW,CAAC,eAAe,EAAE,YAAY,CAAC,GAAG;IACjD,UAAU,CAAC,EAAE,WAAW,CAAC,eAAe,CAAC,YAAY,CAAC,GAAG,OAAO,CAAA;CACjE,CAAC,GACF,SAAS,EACb,aAAa,MAAM,EACnB,UAAS,wBAAwB,GAAG;IAClC,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACnC,IAAI,CAAC,EAAE,OAAO,CAAA;CACV,KACL,iBAAiB,GAAG,SAwDtB,CAAA;AAED,oGAAoG;AACpG,eAAO,MAAM,qBAAqB,GAAI,GAAG,OAAO,EAAE,UAAU,iBAAiB,KAAG,QAY/E,CAAA"}
@@ -25,6 +25,11 @@ export const getStreamingResponse = (mediaType, contentType, options = {}) => {
25
25
  }
26
26
  const itemSchema = typeof mediaType.itemSchema === 'boolean' ? mediaType.itemSchema : getResolvedRefDeep(mediaType.itemSchema);
27
27
  const completeSchema = typeof mediaType.schema === 'boolean' ? mediaType.schema : getResolvedRefDeep(mediaType.schema);
28
+ const generationContext = {
29
+ openapiVersion: options.openapiVersion,
30
+ jsonSchemaDialect: options.jsonSchemaDialect,
31
+ originalOpenapiVersion: options.originalOpenapiVersion,
32
+ };
28
33
  const generateBody = () => {
29
34
  if (itemSchema === false || completeSchema === false) {
30
35
  return [];
@@ -32,15 +37,15 @@ export const getStreamingResponse = (mediaType, contentType, options = {}) => {
32
37
  if (completeSchema === undefined) {
33
38
  return Array.from({ length: 3 }, () => itemSchema === true
34
39
  ? null
35
- : generateResponseExample(coerceValue(SchemaObjectSchema, itemSchema), options.variables));
40
+ : generateResponseExample(coerceValue(SchemaObjectSchema, itemSchema), options.variables, generationContext));
36
41
  }
37
42
  if (typeof completeSchema === 'object' && 'type' in completeSchema && completeSchema.type === 'array') {
38
43
  return generateResponseExample(coerceValue(SchemaObjectSchema, {
39
44
  ...completeSchema,
40
45
  items: ('items' in completeSchema ? completeSchema.items : undefined) ?? itemSchema,
41
- }), options.variables);
46
+ }), options.variables, generationContext);
42
47
  }
43
- return generateResponseExample(coerceValue(SchemaObjectSchema, completeSchema), options.variables);
48
+ return generateResponseExample(coerceValue(SchemaObjectSchema, completeSchema), options.variables, generationContext);
44
49
  };
45
50
  const body = example ? example.value : generateBody();
46
51
  const items = Array.isArray(body) ? body : [body];
@@ -1 +1 @@
1
- {"version":3,"file":"validate-request.d.ts","sourceRoot":"","sources":["../../src/utils/validate-request.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,uBAAuB,CAAA;AAMrE,OAAO,KAAK,EAAW,iBAAiB,EAAE,MAAM,MAAM,CAAA;AA8TtD;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,GAC1B,WAAW,WAAW,CAAC,eAAe,EACtC,qBAAqB,WAAW,CAAC,cAAc,CAAC,YAAY,CAAC,KAC5D,iBAoLF,CAAA"}
1
+ {"version":3,"file":"validate-request.d.ts","sourceRoot":"","sources":["../../src/utils/validate-request.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,uBAAuB,CAAA;AAMrE,OAAO,KAAK,EAAW,iBAAiB,EAAE,MAAM,MAAM,CAAA;AAoUtD;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,GAC1B,WAAW,WAAW,CAAC,eAAe,EACtC,qBAAqB,WAAW,CAAC,cAAc,CAAC,YAAY,CAAC,KAC5D,iBAoLF,CAAA"}
@@ -7,6 +7,7 @@ import Ajv2020 from 'ajv/dist/2020.js';
7
7
  import addFormats from 'ajv-formats';
8
8
  import { getCookie } from 'hono/cookie';
9
9
  import { pathParameters } from './path-parameters.js';
10
+ import { compilePattern } from './compile-pattern.js';
10
11
  import { deserializeArrayParameter, deserializeObjectParameter, getObjectPropertyNames, isArraySchema, isObjectSchema, resolveSerialization, } from './deserialize-parameter.js';
11
12
  import { dropReadOnlyRequired } from './drop-read-only-required.js';
12
13
  import { findQuerystringParameter, getQuerystringJsonSchema, parseQuerystringParameter } from './querystring-parameter.js';
@@ -135,11 +136,16 @@ const compileSchema = (ajv, schema, label) => {
135
136
  */
136
137
  const compileValidators = (operation, pathItemParameters) => {
137
138
  // Coerce string parameters to their declared types (integer, boolean, …).
138
- const parameterAjv = new Ajv2020({ strict: false, allErrors: true, coerceTypes: true });
139
+ const parameterAjv = new Ajv2020({
140
+ strict: false,
141
+ allErrors: true,
142
+ coerceTypes: true,
143
+ code: { regExp: compilePattern },
144
+ });
139
145
  useDecimalMultipleOf(parameterAjv);
140
146
  addFormats(parameterAjv);
141
147
  // Do not coerce the JSON body; a JSON value already carries its type and coercion would mask errors.
142
- const bodyAjv = new Ajv2020({ strict: false, allErrors: true });
148
+ const bodyAjv = new Ajv2020({ strict: false, allErrors: true, code: { regExp: compilePattern } });
143
149
  useDecimalMultipleOf(bodyAjv);
144
150
  addFormats(bodyAjv);
145
151
  // Path-item parameters apply to every operation, so fold them in before building the schemas.
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "swagger",
17
17
  "cli"
18
18
  ],
19
- "version": "0.21.4",
19
+ "version": "0.21.6",
20
20
  "engines": {
21
21
  "node": ">=22.19.0"
22
22
  },
@@ -52,26 +52,26 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@faker-js/faker": "10.6.0",
55
- "@scalar/helpers": "0.18.1",
56
- "@scalar/json-magic": "0.20.1",
57
- "@scalar/json-schema-validator": "0.1.22",
55
+ "@scalar/helpers": "0.19.0",
56
+ "@scalar/json-magic": "0.21.1",
57
+ "@scalar/json-schema-validator": "0.1.23",
58
58
  "@scalar/openapi-types": "0.9.10",
59
- "@scalar/openapi-upgrader": "0.4.11",
60
- "@scalar/types": "0.30.0",
61
- "@scalar/workspace-store": "0.73.0",
59
+ "@scalar/openapi-upgrader": "0.5.1",
60
+ "@scalar/types": "0.31.0",
61
+ "@scalar/workspace-store": "0.75.0",
62
62
  "ajv": "^8.20.0",
63
63
  "ajv-formats": "^3.0.1",
64
- "hono": "^4.13.16",
64
+ "hono": "^4.13.18",
65
65
  "quickjs-emscripten": "0.32.0",
66
66
  "yaml": "^2.9.1"
67
67
  },
68
68
  "devDependencies": {
69
- "@hono/node-server": "^2.1.6",
69
+ "@hono/node-server": "^2.1.8",
70
70
  "@types/node": "^24.19.2",
71
71
  "@types/ws": "8.18.2",
72
72
  "undici": "^8.12.4",
73
- "vite": "8.3.4",
74
- "ws": "8.22.0"
73
+ "vite": "8.3.8",
74
+ "ws": "8.23.0"
75
75
  },
76
76
  "scripts": {
77
77
  "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",