@oslo-flanders/swagger-generator 3.0.0 → 3.1.0
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/lib/SwaggerGenerationService.d.ts +8 -0
- package/lib/SwaggerGenerationService.js +72 -0
- package/lib/SwaggerGenerationServiceRunner.js +5 -0
- package/lib/config/SwaggerGenerationServiceConfiguration.d.ts +5 -0
- package/lib/config/SwaggerGenerationServiceConfiguration.js +4 -0
- package/package.json +1 -1
|
@@ -17,4 +17,12 @@ export declare class SwaggerGenerationService implements IService {
|
|
|
17
17
|
createSchemas(): Object;
|
|
18
18
|
createLinks(): Object;
|
|
19
19
|
writeOutput(outputFormat: OutputFormat, obj: Object, outputPath: string): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Create a self-contained copy of the components object where all `$ref`
|
|
22
|
+
* pointers to `#/components/schemas/<name>` are replaced by the actual
|
|
23
|
+
* schema definition inline. Resolves recursively so that nested `$ref`
|
|
24
|
+
* references are also inlined. Circular references are preserved as `$ref`
|
|
25
|
+
* to avoid infinite recursion.
|
|
26
|
+
*/
|
|
27
|
+
private resolveRefs;
|
|
20
28
|
}
|
|
@@ -128,6 +128,10 @@ let SwaggerGenerationService = class SwaggerGenerationService {
|
|
|
128
128
|
...(Object.keys(links).length > 0 ? { links } : {}),
|
|
129
129
|
},
|
|
130
130
|
};
|
|
131
|
+
/* Create embedded (self-contained) variant with all $ref resolved inline */
|
|
132
|
+
const embeddedComponents = this.configuration.generateEmbedded
|
|
133
|
+
? this.resolveRefs(components, schemas)
|
|
134
|
+
: undefined;
|
|
131
135
|
/* Create Swagger endpoint paths as example */
|
|
132
136
|
const swagger = this.createSwagger(schemas, links);
|
|
133
137
|
/* Only append the language suffix when the generated language differs from the primary language */
|
|
@@ -141,6 +145,9 @@ let SwaggerGenerationService = class SwaggerGenerationService {
|
|
|
141
145
|
for (const label of Object.keys(links))
|
|
142
146
|
await this.writeOutput(format, links[label], `swagger/components/links/${label}${languageSuffix}${ext}`);
|
|
143
147
|
await this.writeOutput(format, components, `swagger/components${languageSuffix}${ext}`);
|
|
148
|
+
if (embeddedComponents) {
|
|
149
|
+
await this.writeOutput(format, embeddedComponents, `swagger/_embedded${languageSuffix}${ext}`);
|
|
150
|
+
}
|
|
144
151
|
await this.writeOutput(format, swagger, `swagger/example${languageSuffix}${ext}`);
|
|
145
152
|
}
|
|
146
153
|
}
|
|
@@ -596,6 +603,71 @@ let SwaggerGenerationService = class SwaggerGenerationService {
|
|
|
596
603
|
(0, core_1.ensureOutputDirectory)(path.dirname(filePath));
|
|
597
604
|
await (0, promises_1.writeFile)(filePath, data);
|
|
598
605
|
}
|
|
606
|
+
/**
|
|
607
|
+
* Create a self-contained copy of the components object where all `$ref`
|
|
608
|
+
* pointers to `#/components/schemas/<name>` are replaced by the actual
|
|
609
|
+
* schema definition inline. Resolves recursively so that nested `$ref`
|
|
610
|
+
* references are also inlined. Circular references are preserved as `$ref`
|
|
611
|
+
* to avoid infinite recursion.
|
|
612
|
+
*/
|
|
613
|
+
resolveRefs(components, schemas) {
|
|
614
|
+
const embedded = JSON.parse(JSON.stringify(components));
|
|
615
|
+
const resolving = new Set();
|
|
616
|
+
function walk(obj) {
|
|
617
|
+
if (!obj || typeof obj !== 'object') {
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
if (Array.isArray(obj)) {
|
|
621
|
+
for (let i = 0; i < obj.length; i++) {
|
|
622
|
+
if (obj[i] !== null && typeof obj[i] === 'object' && obj[i].$ref) {
|
|
623
|
+
const match = obj[i].$ref.match(/^#\/components\/schemas\/(.+)$/);
|
|
624
|
+
if (match && schemas[match[1]]) {
|
|
625
|
+
if (resolving.has(match[1])) {
|
|
626
|
+
// Circular reference – keep the $ref as-is
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
resolving.add(match[1]);
|
|
630
|
+
obj[i] = JSON.parse(JSON.stringify(schemas[match[1]]));
|
|
631
|
+
walk(obj[i]);
|
|
632
|
+
resolving.delete(match[1]);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
walk(obj[i]);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
for (const key of Object.keys(obj)) {
|
|
642
|
+
if (key === '$ref' &&
|
|
643
|
+
typeof obj[key] === 'string' &&
|
|
644
|
+
obj.$ref.startsWith('#/components/schemas/')) {
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
if (obj[key] !== null &&
|
|
648
|
+
typeof obj[key] === 'object' &&
|
|
649
|
+
obj[key].$ref) {
|
|
650
|
+
const match = obj[key].$ref.match(/^#\/components\/schemas\/(.+)$/);
|
|
651
|
+
if (match && schemas[match[1]]) {
|
|
652
|
+
if (resolving.has(match[1])) {
|
|
653
|
+
// Circular reference – keep the $ref as-is
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
resolving.add(match[1]);
|
|
657
|
+
obj[key] = JSON.parse(JSON.stringify(schemas[match[1]]));
|
|
658
|
+
walk(obj[key]);
|
|
659
|
+
resolving.delete(match[1]);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
else {
|
|
663
|
+
walk(obj[key]);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
walk(embedded);
|
|
669
|
+
return embedded;
|
|
670
|
+
}
|
|
599
671
|
};
|
|
600
672
|
SwaggerGenerationService = __decorate([
|
|
601
673
|
(0, inversify_1.injectable)(),
|
|
@@ -50,6 +50,11 @@ class SwaggerGenerationServiceRunner extends core_1.AppRunner {
|
|
|
50
50
|
describe: 'Disable the creation of links.',
|
|
51
51
|
default: false,
|
|
52
52
|
boolean: true,
|
|
53
|
+
})
|
|
54
|
+
.option('generateEmbedded', {
|
|
55
|
+
describe: 'Generate the embedded (self-contained) variant with all $ref resolved inline.',
|
|
56
|
+
default: false,
|
|
57
|
+
boolean: true,
|
|
53
58
|
})
|
|
54
59
|
.option('expanded', {
|
|
55
60
|
describe: 'Use JSON-LD expanded format instead of default compact format.',
|
|
@@ -78,6 +78,10 @@ export declare class SwaggerGenerationServiceConfiguration implements IConfigura
|
|
|
78
78
|
* Whether to disable the creation of links
|
|
79
79
|
*/
|
|
80
80
|
private _disableLinks;
|
|
81
|
+
/**
|
|
82
|
+
* Whether to generate the embedded (self-contained) variant with all $ref resolved inline
|
|
83
|
+
*/
|
|
84
|
+
private _generateEmbedded;
|
|
81
85
|
/**
|
|
82
86
|
* Whether to use expanded JSON-LD format or not
|
|
83
87
|
*/
|
|
@@ -110,6 +114,7 @@ export declare class SwaggerGenerationServiceConfiguration implements IConfigura
|
|
|
110
114
|
get excludeProperties(): string[];
|
|
111
115
|
get outputFormat(): OutputFormat[];
|
|
112
116
|
get disableLinks(): boolean;
|
|
117
|
+
get generateEmbedded(): boolean;
|
|
113
118
|
get expanded(): boolean;
|
|
114
119
|
get excludeClassesExpanded(): string[];
|
|
115
120
|
get excludePropertiesExpanded(): string[];
|
|
@@ -30,6 +30,7 @@ let SwaggerGenerationServiceConfiguration = class SwaggerGenerationServiceConfig
|
|
|
30
30
|
this._excludeProperties = params.excludeProperties;
|
|
31
31
|
this._outputFormat = params.outputFormat;
|
|
32
32
|
this._disableLinks = params.disableLinks;
|
|
33
|
+
this._generateEmbedded = params.generateEmbedded;
|
|
33
34
|
this._expanded = params.expanded;
|
|
34
35
|
this._excludeClassesExpanded = params.excludeClassesExpanded;
|
|
35
36
|
this._excludePropertiesExpanded = (params.excludePropertiesExpanded);
|
|
@@ -116,6 +117,9 @@ let SwaggerGenerationServiceConfiguration = class SwaggerGenerationServiceConfig
|
|
|
116
117
|
get disableLinks() {
|
|
117
118
|
return !!this._disableLinks;
|
|
118
119
|
}
|
|
120
|
+
get generateEmbedded() {
|
|
121
|
+
return !!this._generateEmbedded;
|
|
122
|
+
}
|
|
119
123
|
get expanded() {
|
|
120
124
|
return !!this._expanded;
|
|
121
125
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oslo-flanders/swagger-generator",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Generate OpenAPI Swagger YAML documentation.",
|
|
5
5
|
"author": "Digitaal Vlaanderen <https://data.vlaanderen.be/id/organisatie/OVO002949>",
|
|
6
6
|
"homepage": "https://github.com/informatievlaanderen/OSLO-UML-Transformer/tree/main/packages/oslo-generator-swagger#readme",
|