Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit f060bb9

Browse files
xcasselCasselX
authored andcommitted
Feature: allow passing "patch" option to "swagger2openapi" (acacode#283)
Co-authored-by: cassel <[email protected]>
1 parent b7c7681 commit f060bb9

File tree

10 files changed

+29134
-7
lines changed

10 files changed

+29134
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Options:
6363
--default-response <type> default type for empty response schema (default: "void")
6464
--type-prefix <string> data contract name prefix (default: "")
6565
--type-suffix <string> data contract name suffix (default: "")
66+
--patch fix up small errors in the swagger source definition (default: false)
6667
-h, --help display help for command
6768
```
6869

index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ interface GenerateApiParams {
112112
* extra templates
113113
*/
114114
extraTemplates?: { name: string; path: string }[];
115+
116+
/**
117+
* fix up small errors in the swagger source definition
118+
*/
119+
patch?: boolean;
115120
}
116121

117122
export interface Hooks {

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ program
7676
"--clean-output",
7777
"clean output folder before generate api. WARNING: May cause data loss",
7878
false,
79-
);
79+
)
80+
.option("--patch", "fix up small errors in the swagger source definition", false);
8081

8182
program.parse(process.argv);
8283

@@ -107,6 +108,7 @@ const {
107108
silent,
108109
typePrefix,
109110
typeSuffix,
111+
patch,
110112
} = program;
111113

112114
generateApi({
@@ -137,4 +139,5 @@ generateApi({
137139
silent: !!silent,
138140
typePrefix,
139141
typeSuffix,
142+
patch: !!patch,
140143
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"test:--axios--single-http-client": "node tests/spec/axiosSingleHttpClient/test.js",
4040
"test:--type-suffix--type-prefix": "node tests/spec/typeSuffixPrefix/test.js",
4141
"test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
42-
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js"
42+
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js",
43+
"test:--patch": "node tests/spec/patch/test.js"
4344
},
4445
"author": "acacode",
4546
"license": "MIT",

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const config = {
8383
silent: false,
8484
typePrefix: "",
8585
typeSuffix: "",
86+
patch: false,
8687
componentTypeNameResolver: new NameResolver([]),
8788
};
8889

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ module.exports = {
5656
silent = config.silent,
5757
typePrefix = config.typePrefix,
5858
typeSuffix = config.typeSuffix,
59+
patch = config.patch,
5960
}) =>
6061
new Promise((resolve, reject) => {
6162
addToConfig({
@@ -85,10 +86,11 @@ module.exports = {
8586
toJS: translateToJavaScript,
8687
typePrefix,
8788
typeSuffix,
89+
patch,
8890
});
8991
(spec
90-
? convertSwaggerObject(spec)
91-
: getSwaggerObject(input, url, disableStrictSSL, disableProxy)
92+
? convertSwaggerObject(spec, { patch })
93+
: getSwaggerObject(input, url, disableStrictSSL, disableProxy, { patch })
9294
)
9395
.then(({ usageSchema, originalSchema }) => {
9496
const templatePaths = getTemplatePaths(config);

src/swagger.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ const getSwaggerFile = (pathToSwagger, urlToSwagger, disableStrictSSL, disablePr
4242
}
4343
});
4444

45-
const getSwaggerObject = (pathToSwagger, urlToSwagger, disableStrictSSL, disableProxy) =>
45+
const getSwaggerObject = (
46+
pathToSwagger,
47+
urlToSwagger,
48+
disableStrictSSL,
49+
disableProxy,
50+
converterOptions,
51+
) =>
4652
getSwaggerFile(pathToSwagger, urlToSwagger, disableStrictSSL, disableProxy).then((file) =>
47-
convertSwaggerObject(parseSwaggerFile(file)),
53+
convertSwaggerObject(parseSwaggerFile(file), converterOptions),
4854
);
4955

50-
const convertSwaggerObject = (swaggerSchema) => {
56+
const convertSwaggerObject = (swaggerSchema, converterOptions) => {
5157
return new Promise((resolve) => {
5258
swaggerSchema.info = _.merge(
5359
{
@@ -63,6 +69,7 @@ const convertSwaggerObject = (swaggerSchema) => {
6369
converter.convertObj(
6470
swaggerSchema,
6571
{
72+
...converterOptions,
6673
warnOnly: true,
6774
refSiblings: "preserve",
6875
rbname: "requestBodyName",

0 commit comments

Comments
 (0)