Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: allow passing "patch" option to "swagger2openapi" #283

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Options:
--default-response <type> default type for empty response schema (default: "void")
--type-prefix <string> data contract name prefix (default: "")
--type-suffix <string> data contract name suffix (default: "")
--patch fix up small errors in the swagger source definition (default: false)
-h, --help display help for command
```

Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ interface GenerateApiParams {
* extra templates
*/
extraTemplates?: { name: string; path: string }[];

/**
* fix up small errors in the swagger source definition
*/
patch?: boolean;
}

export interface Hooks {
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ program
"--clean-output",
"clean output folder before generate api. WARNING: May cause data loss",
false,
);
)
.option("--patch", "fix up small errors in the swagger source definition", false);

program.parse(process.argv);

Expand Down Expand Up @@ -107,6 +108,7 @@ const {
silent,
typePrefix,
typeSuffix,
patch,
} = program;

generateApi({
Expand Down Expand Up @@ -137,4 +139,5 @@ generateApi({
silent: !!silent,
typePrefix,
typeSuffix,
patch: !!patch,
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"test:--axios--single-http-client": "node tests/spec/axiosSingleHttpClient/test.js",
"test:--type-suffix--type-prefix": "node tests/spec/typeSuffixPrefix/test.js",
"test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js"
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js",
"test:--patch": "node tests/spec/patch/test.js"
},
"author": "acacode",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const config = {
silent: false,
typePrefix: "",
typeSuffix: "",
patch: false,
componentTypeNameResolver: new NameResolver([]),
};

Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = {
silent = config.silent,
typePrefix = config.typePrefix,
typeSuffix = config.typeSuffix,
patch = config.patch,
}) =>
new Promise((resolve, reject) => {
addToConfig({
Expand Down Expand Up @@ -85,10 +86,11 @@ module.exports = {
toJS: translateToJavaScript,
typePrefix,
typeSuffix,
patch,
});
(spec
? convertSwaggerObject(spec)
: getSwaggerObject(input, url, disableStrictSSL, disableProxy)
? convertSwaggerObject(spec, { patch })
: getSwaggerObject(input, url, disableStrictSSL, disableProxy, { patch })
)
.then(({ usageSchema, originalSchema }) => {
const templatePaths = getTemplatePaths(config);
Expand Down
13 changes: 10 additions & 3 deletions src/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ const getSwaggerFile = (pathToSwagger, urlToSwagger, disableStrictSSL, disablePr
}
});

const getSwaggerObject = (pathToSwagger, urlToSwagger, disableStrictSSL, disableProxy) =>
const getSwaggerObject = (
pathToSwagger,
urlToSwagger,
disableStrictSSL,
disableProxy,
converterOptions,
) =>
getSwaggerFile(pathToSwagger, urlToSwagger, disableStrictSSL, disableProxy).then((file) =>
convertSwaggerObject(parseSwaggerFile(file)),
convertSwaggerObject(parseSwaggerFile(file), converterOptions),
);

const convertSwaggerObject = (swaggerSchema) => {
const convertSwaggerObject = (swaggerSchema, converterOptions) => {
return new Promise((resolve) => {
swaggerSchema.info = _.merge(
{
Expand All @@ -63,6 +69,7 @@ const convertSwaggerObject = (swaggerSchema) => {
converter.convertObj(
swaggerSchema,
{
...converterOptions,
warnOnly: true,
refSiblings: "preserve",
rbname: "requestBodyName",
Expand Down
Loading