Skip to content

Commit 1053f2a

Browse files
committed
Continue implementing OpenAPI 3.2.0
1 parent dfcb461 commit 1053f2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3330
-734
lines changed

src/core/constants.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,14 @@ export const SERVICE_INDEX_GENERATOR_HEADER_COMMENT = HEADER;
99
export const MAIN_INDEX_GENERATOR_HEADER_COMMENT = HEADER;
1010
export const PROVIDER_GENERATOR_HEADER_COMMENT = HEADER;
1111
export const UTILITY_GENERATOR_HEADER_COMMENT = HEADER;
12+
13+
/**
14+
* The standard JSON Schema dialect identifier used by OpenAPI 3.1.
15+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#schema-vocabulary
16+
*/
17+
export const OAS_3_1_DIALECT = 'https://spec.openapis.org/oas/3.1/dialect/base';
18+
19+
/**
20+
* The standard JSON Schema dialect identifier for JSON Schema 2020-12, which OAS 3.1 is based on.
21+
*/
22+
export const JSON_SCHEMA_2020_12_DIALECT = 'https://json-schema.org/draft/2020-12/schema';

src/core/parser.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { pathToFileURL } from 'node:url';
1919
import yaml from 'js-yaml';
2020
import { extractPaths, isUrl, pascalCase } from './utils.js';
2121
import { validateSpec } from './validator.js';
22+
import { JSON_SCHEMA_2020_12_DIALECT, OAS_3_1_DIALECT } from './constants.js';
2223

2324
/** Represents a `$ref` object in a JSON Schema. */
2425
interface RefObject {
@@ -91,7 +92,18 @@ export class SwaggerParser {
9192
// 1. Fundamental Structure Validation
9293
validateSpec(spec);
9394

94-
// 2. User-Configured Custom Validation
95+
// 2. Dialect Validation (OAS 3.1+)
96+
if (spec.jsonSchemaDialect) {
97+
const dialect = spec.jsonSchemaDialect;
98+
// We accept the specific OAS dialect and the generic JSON Schema Draft 2020-12
99+
if (dialect !== OAS_3_1_DIALECT && dialect !== JSON_SCHEMA_2020_12_DIALECT) {
100+
console.warn(`⚠️ Warning: The specification defines a custom jsonSchemaDialect: "${dialect}". ` +
101+
`This generator is optimized for the default OpenAPI 3.1 dialect (${OAS_3_1_DIALECT}). ` +
102+
`Some schema features may not be generated exactly as intended.`);
103+
}
104+
}
105+
106+
// 3. User-Configured Custom Validation
95107
if (config.validateInput && !config.validateInput(spec)) {
96108
throw new Error("Custom input validation failed.");
97109
}
@@ -277,7 +289,7 @@ export class SwaggerParser {
277289
}
278290

279291
/**
280-
* Synchronously resolves a JSON reference (`$ref`) object to its corresponding definition.
292+
* Synchronously resolves a JSON reference (`$ref`) object to its definition.
281293
* If the provided object is not a `$ref`, it is returned as is.
282294
* This method assumes all necessary files have been pre-loaded into the cache.
283295
* @template T The expected type of the resolved object.

src/core/types.ts

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { ModuleKind, ScriptTarget } from "ts-morph";
1717

1818
/**
1919
* License information for the exposed API.
20-
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#licenseObject
20+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#licenseObject
2121
*/
2222
export interface LicenseObject {
2323
/** The license name used for the API. */
@@ -30,7 +30,7 @@ export interface LicenseObject {
3030

3131
/**
3232
* Contact information for the exposed API.
33-
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#contactObject
33+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#contactObject
3434
*/
3535
export interface ContactObject {
3636
/** The identifying name of the contact person/organization. */
@@ -43,7 +43,7 @@ export interface ContactObject {
4343

4444
/**
4545
* The object provides metadata about the API.
46-
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#infoObject
46+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#infoObject
4747
*/
4848
export interface InfoObject {
4949
/** The title of the API. */
@@ -62,6 +62,32 @@ export interface InfoObject {
6262
version: string;
6363
}
6464

65+
/**
66+
* Allows referencing an external resource for extended documentation.
67+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#externalDocumentationObject
68+
*/
69+
export interface ExternalDocumentationObject {
70+
/** A description of the target documentation. */
71+
description?: string;
72+
/** The URL for the target documentation. */
73+
url: string;
74+
}
75+
76+
/**
77+
* Adds metadata to a single tag that is used by the Operation Object.
78+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#tagObject
79+
*/
80+
export interface TagObject {
81+
/** The name of the tag. */
82+
name: string;
83+
/** A short summary of the tag. (OAS 3.1+) */
84+
summary?: string;
85+
/** A description for the tag. */
86+
description?: string;
87+
/** Additional external documentation for this tag. */
88+
externalDocs?: ExternalDocumentationObject;
89+
}
90+
6591
/**
6692
* An object representing a Server Variable for server URL template substitution.
6793
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#server-variable-object
@@ -100,7 +126,7 @@ export interface DiscriminatorObject {
100126

101127
/**
102128
* Metadata object that allows for more fine-tuned XML model definitions.
103-
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#xmlObject
129+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#xmlObject
104130
*/
105131
export interface XmlObject {
106132
/** Replaces the name of the element/attribute used for the described schema property. */
@@ -109,28 +135,26 @@ export interface XmlObject {
109135
namespace?: string;
110136
/** The prefix to be used for the name. */
111137
prefix?: string;
112-
/** Declares whether the property definition translates to an attribute instead of an element. */
138+
/**
139+
* Declares whether the property definition translates to an attribute instead of an element.
140+
* @deprecated Use `nodeType: 'attribute'` instead.
141+
*/
113142
attribute?: boolean;
114-
/** MAY be used only for an array definition. Signifies whether the array is wrapped. */
143+
/**
144+
* MAY be used only for an array definition. Signifies whether the array is wrapped.
145+
* @deprecated Use `nodeType: 'element'` (on the array) for wrapping, partial/implicit 'none' for unwrapped.
146+
*/
115147
wrapped?: boolean;
116-
/** Node type for XML mapping (OpenAPI 3.2.0 / Extended spec). */
117-
nodeType?: string;
118-
}
119-
120-
/**
121-
* Allows referencing an external resource for extended documentation.
122-
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#externalDocumentationObject
123-
*/
124-
export interface ExternalDocumentationObject {
125-
/** A description of the target documentation. */
126-
description?: string;
127-
/** The URL for the target documentation. */
128-
url: string;
148+
/**
149+
* Node type for XML mapping (OpenAPI 3.2.0).
150+
* Values: 'element' | 'attribute' | 'text' | 'cdata' | 'none'.
151+
*/
152+
nodeType?: 'element' | 'attribute' | 'text' | 'cdata' | 'none' | string;
129153
}
130154

131155
/**
132156
* The Link Object represents a possible design-time link for a response.
133-
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#linkObject
157+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#linkObject
134158
*/
135159
export interface LinkObject {
136160
/** A URI reference to an OAS operation. Mutually exclusive with operationId. */
@@ -151,7 +175,7 @@ export interface LinkObject {
151175
* The Header Object follows the structure of the Parameter Object with the following changes:
152176
* 1. `name` MUST NOT be specified, it is given in the corresponding `headers` map.
153177
* 2. `in` MUST NOT be specified, it is implicitly in `header`.
154-
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#headerObject
178+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#headerObject
155179
*/
156180
export interface HeaderObject {
157181
description?: string;
@@ -236,9 +260,9 @@ export interface PathInfo {
236260
/** Security requirements specific to this operation. keys are definitions, values are scopes. */
237261
security?: { [key: string]: string[] }[];
238262
/** An alternate server array to service this operation. (OAS 3+) */
239-
servers?: ServerObject[] | undefined; // Allow undefined explicit assignment
263+
servers?: ServerObject[] | undefined;
240264
/** A map of possible out-of band callbacks related to the parent operation. (OAS 3+) */
241-
callbacks?: Record<string, PathItem | { $ref: string }> | undefined; // Allow undefined explicit assignment
265+
callbacks?: Record<string, PathItem | { $ref: string }>;
242266
}
243267

244268
/** A single encoding definition for a multipart property. */
@@ -282,20 +306,30 @@ export interface SwaggerResponse {
282306
/**
283307
* A normalized and extended interface representing an OpenAPI Schema Object.
284308
* This is the core structure for defining data models.
309+
*
310+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#schemaObject
285311
*/
286312
export interface SwaggerDefinition {
287313
type?: "string" | "number" | "integer" | "boolean" | "object" | "array" | "file" | "null" | ("string" | "number" | "integer" | "boolean" | "object" | "array" | "null")[];
288314
format?: string;
289315
description?: string;
290316
default?: unknown;
317+
/** Specifies that a parameter is deprecated and SHOULD be transitioned out of usage. */
318+
deprecated?: boolean;
291319
/** JSON Schema `const` keyword. (OAS 3.1 / JSON Schema 2020-12) */
292320
const?: unknown;
293321
maximum?: number;
294-
/** If true, the `maximum` is exclusive. */
295-
exclusiveMaximum?: boolean;
322+
/**
323+
* If boolean (OAS 3.0/Swagger 2): If true, `maximum` is exclusive.
324+
* If number (OAS 3.1+/JSON Schema 2020-12): The exclusive maximum value.
325+
*/
326+
exclusiveMaximum?: boolean | number;
296327
minimum?: number;
297-
/** If true, the `minimum` is exclusive. */
298-
exclusiveMinimum?: boolean;
328+
/**
329+
* If boolean (OAS 3.0/Swagger 2): If true, `minimum` is exclusive.
330+
* If number (OAS 3.1+/JSON Schema 2020-12): The exclusive minimum value.
331+
*/
332+
exclusiveMinimum?: boolean | number;
299333
maxLength?: number;
300334
minLength?: number;
301335
pattern?: string;
@@ -328,8 +362,15 @@ export interface SwaggerDefinition {
328362
writeOnly?: boolean;
329363
nullable?: boolean;
330364
required?: string[];
331-
/** An example of the schema representation. */
365+
/**
366+
* An example of the schema representation.
367+
* @deprecated exist in OAS 3.2 in favor of `examples`
368+
*/
332369
example?: unknown;
370+
/**
371+
* Free-form field to include examples of instances for this schema. (OAS 3.1+)
372+
*/
373+
examples?: unknown[];
333374

334375
xml?: XmlObject;
335376
externalDocs?: ExternalDocumentationObject;
@@ -383,17 +424,30 @@ export interface PathItem {
383424
patch?: SpecOperation;
384425
trace?: SpecOperation;
385426
query?: SpecOperation; // OAS 3.2 draft
427+
/**
428+
* A map of additional operations on this path (OAS 3.2).
429+
* Keys are HTTP methods (e.g., COPY, LOCK).
430+
*/
431+
additionalOperations?: Record<string, SpecOperation>;
386432
parameters?: any[];
387433
servers?: ServerObject[]; // OAS 3 (Path-level override)
388434
[key: string]: any;
389435
}
390436

391-
/** The root object of a parsed OpenAPI/Swagger specification. */
437+
/**
438+
* The root object of a parsed OpenAPI/Swagger specification.
439+
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#oasObject
440+
*/
392441
export interface SwaggerSpec {
393442
openapi?: string;
394443
swagger?: string;
395444
$self?: string;
396445
info: InfoObject;
446+
/** Additional external documentation. */
447+
externalDocs?: ExternalDocumentationObject;
448+
/** A list of tags used by the OpenAPI Description with additional metadata. */
449+
tags?: TagObject[];
450+
397451
paths: { [pathName: string]: PathItem };
398452
/** The incoming webhooks that MAY be received as part of this API. */
399453
webhooks?: { [name: string]: PathItem };
@@ -408,6 +462,7 @@ export interface SwaggerSpec {
408462
schemas?: Record<string, SwaggerDefinition>;
409463
securitySchemes?: Record<string, SecurityScheme>;
410464
pathItems?: Record<string, PathItem>;
465+
callbacks?: Record<string, PathItem | { $ref: string }>;
411466
links?: Record<string, LinkObject | { $ref: string }>;
412467
headers?: Record<string, HeaderObject | { $ref: string }>;
413468
};

0 commit comments

Comments
 (0)