Skip to content

Commit

Permalink
Release 1.7.2 (#43)
Browse files Browse the repository at this point in the history
* bugfix: converting inline object into name of type for request body

* fix: bug when path parameters is not set but contains in endpoint url.

Co-authored-by: svolkov <[email protected]>
  • Loading branch information
js2me and js2me authored Mar 29, 2020
1 parent e56d604 commit 55529b8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 24 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# next release

# 1.7.2

Fixes:

- Critical bug with converting inline object into name of type for request body.
- Fix bug when path parameters is not set but contains in endpoint url.
![path params bug 1](./assets/changelog_assets/bug-with-no-path-args.jpg)
![path params bug 2](./assets/changelog_assets/bug-with-no-path-args2.jpg)


# 1.7.0

Breaking Changes:
Expand Down
Binary file added assets/changelog_assets/bug-with-no-path-args.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-typescript-api",
"version": "1.7.0",
"version": "1.7.2",
"description": "Create typescript api module from swagger schema",
"scripts": {
"cli": "node index.js -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
Expand Down
70 changes: 48 additions & 22 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ const methodAliases = {
delete: (pathName, hasPathInserts) => _.camelCase(`${pathName}_delete`),
};

const getSchemaFromRequestType = requestType => {
const getSchemaFromRequestType = (requestType) => {
const content = _.get(requestType, "content");

if (!content) return null;

const contentByType = _.find(content, contentByType => contentByType.schema);
const contentByType = _.find(content, (contentByType) => contentByType.schema);

return contentByType && contentByType.schema;
};
Expand All @@ -40,15 +40,15 @@ const getTypeFromRequestInfo = (requestInfo, parsedSchemas, operationId, content
const { content } = parseSchema(schema, "none", inlineExtraFormatters);
const foundedSchemaByName = _.find(
parsedSchemas,
parsedSchema => parsedSchema.name === content,
(parsedSchema) => parsedSchema.name === content,
);
const foundSchemaByContent = _.find(parsedSchemas, parsedSchema =>
const foundSchemaByContent = _.find(parsedSchemas, (parsedSchema) =>
_.isEqual(parsedSchema.content, content),
);

const foundSchema = foundedSchemaByName || foundSchemaByContent;

return checkAndRenameModelName(foundSchema ? foundSchema.name : content);
return foundSchema ? checkAndRenameModelName(foundSchema.name) : content;
}

if (refTypeInfo) {
Expand All @@ -57,7 +57,7 @@ const getTypeFromRequestInfo = (requestInfo, parsedSchemas, operationId, content

// TODO:HACK fix problem of swagger2opeanpi
const typeNameWithoutOpId = _.replace(refTypeInfo.typeName, operationId, "");
if (_.find(parsedSchemas, schema => schema.name === typeNameWithoutOpId))
if (_.find(parsedSchemas, (schema) => schema.name === typeNameWithoutOpId))
return checkAndRenameModelName(typeNameWithoutOpId);

switch (refTypeInfo.componentName) {
Expand Down Expand Up @@ -95,14 +95,14 @@ const getTypesFromResponses = (responses, parsedSchemas, operationId) =>
[],
);

const isSuccessResponseStatus = status =>
const isSuccessResponseStatus = (status) =>
(config.defaultResponseAsSuccess && status === "default") ||
(+status >= SUCCESS_RESPONSE_STATUS_RANGE[0] && +status < SUCCESS_RESPONSE_STATUS_RANGE[1]);

const findBadResponses = responses =>
const findBadResponses = (responses) =>
_.filter(responses, (v, status) => !isSuccessResponseStatus(status));

const findSuccessResponse = responses =>
const findSuccessResponse = (responses) =>
_.find(responses, (v, status) => isSuccessResponseStatus(status));

const getReturnType = (responses, parsedSchemas, operationId) =>
Expand All @@ -112,8 +112,8 @@ const getReturnType = (responses, parsedSchemas, operationId) =>
const getErrorReturnType = (responses, parsedSchemas, operationId) =>
_.uniq(
findBadResponses(responses)
.map(response => getTypeFromRequestInfo(response, parsedSchemas, operationId))
.filter(type => type !== DEFAULT_PRIMITIVE_TYPE),
.map((response) => getTypeFromRequestInfo(response, parsedSchemas, operationId))
.filter((type) => type !== DEFAULT_PRIMITIVE_TYPE),
).join(" | ") || DEFAULT_PRIMITIVE_TYPE;

const createCustomOperationId = (method, route, moduleName) => {
Expand Down Expand Up @@ -168,13 +168,13 @@ const parseRoutes = ({ paths }, parsedSchemas) =>
responses,
} = requestInfo;
const hasSecurity = !!(security && security.length);
const pathParams = collect(parameters, parameter => {
const pathParams = collect(parameters, (parameter) => {
if (parameter.in === "path") return parameter;

const refTypeInfo = getRefType(parameter);
return refTypeInfo && refTypeInfo.rawTypeData.in === "path" && refTypeInfo.rawTypeData;
});
const queryParams = collect(parameters, parameter => {
const queryParams = collect(parameters, (parameter) => {
if (parameter.in === "query") return parameter;

const refTypeInfo = getRefType(parameter);
Expand Down Expand Up @@ -216,16 +216,40 @@ const parseRoutes = ({ paths }, parsedSchemas) =>
? getTypeFromRequestInfo(requestBody, parsedSchemas, operationId)
: null;

const pathArgs = _.map(pathParams, param => ({
name: param.name,
optional: !param.required,
type: parseSchema(param.schema, null, inlineExtraFormatters).content,
}));
// Gets all in path parameters from route
// Example: someurl.com/{id}/{name}
// returns: ["id", "name"]
const insideRoutePathArgs = _.compact(
_.split(route, "{").map((part) => (part.includes("}") ? part.split("}")[0] : null)),
);

// Path args - someurl.com/{id}/{name}
// id, name its path args
const pathArgs = insideRoutePathArgs.length
? _.map(pathParams, (param) => ({
name: param.name,
optional: !param.required,
type: parseSchema(param.schema, null, inlineExtraFormatters).content,
}))
: [];

insideRoutePathArgs.forEach((routePathArg) => {
// Cases when in path parameters is not exist in "parameters"
if (!pathArgs.find((pathArg) => pathArg && pathArg.name === routePathArg)) {
pathArgs.push({
name: routePathArg,
optional: false,
type: "string",
});
}
});

const specificArgs = {
query: queryType
? {
name: pathArgs.some(pathArg => pathArg.name === "query") ? "queryParams" : "query",
name: pathArgs.some((pathArg) => pathArg.name === "query")
? "queryParams"
: "query",
optional: parseSchema(queryObjectSchema, null).allFieldsAreOptional,
type: queryType,
}
Expand All @@ -239,15 +263,17 @@ const parseRoutes = ({ paths }, parsedSchemas) =>
}
: void 0,
requestParams: {
name: pathArgs.some(pathArg => pathArg.name === "params") ? "requestParams" : "params",
name: pathArgs.some((pathArg) => pathArg.name === "params")
? "requestParams"
: "params",
optional: true,
type: "RequestParams",
},
};

let routeArgs = [...pathArgs, specificArgs.query, specificArgs.body].filter(Boolean);

if (routeArgs.some(pathArg => pathArg.optional)) {
if (routeArgs.some((pathArg) => pathArg.optional)) {
const { optionalArgs, requiredArgs } = _.reduce(
[...routeArgs],
(acc, pathArg) => {
Expand Down Expand Up @@ -326,7 +352,7 @@ const parseRoutes = ({ paths }, parsedSchemas) =>
];
}, []);

const groupRoutes = routes => {
const groupRoutes = (routes) => {
const duplicates = {};
return _.reduce(
routes.reduce(
Expand Down

0 comments on commit 55529b8

Please sign in to comment.