Skip to content

Commit

Permalink
feat: add parameters in path support
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
Anton Koshkin committed Dec 10, 2018
1 parent 6ee7070 commit 9720eb0
Show file tree
Hide file tree
Showing 7 changed files with 651 additions and 638 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const generate = async (options: TGenerateOptions): Promise<void> => {
const report = PathReporter.report(decoded);
const lastReport = last(report);
log(lastReport.getOrElse('Invalid spec'));
fs.writeFileSync(`${options.out}/qwe.txt`, decoded.toString());
ThrowReporter.report(decoded);
return;
}
Expand Down
7 changes: 4 additions & 3 deletions src/language/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TDefinitionsObject,
TNonArrayItemsObject,
TOperationObject,
TParametersDefinitionsObject,
TPathItemObject,
TPathParameterObject,
TPathsObject,
Expand Down Expand Up @@ -147,7 +148,7 @@ export const serialize: TSerializer = (name: string, swaggerObject: TSwaggerObje
directory(CLIENT_DIRECTORY, [file(`${CLIENT_FILENAME}.ts`, client)]),
directory(UTILS_DIRECTORY, [file(`${UTILS_FILENAME}.ts`, utils)]),
...catOptions([swaggerObject.definitions.map(serializeDefinitions)]),
serializePaths(swaggerObject.paths),
serializePaths(swaggerObject.paths, swaggerObject.parameters),
]);

const serializeDefinitions = (definitions: TDefinitionsObject): TDirectory =>
Expand All @@ -156,10 +157,10 @@ const serializeDefinitions = (definitions: TDefinitionsObject): TDirectory =>
serializeDefinition(name, definition, `${ROOT_DIRECTORY}/${DEFINITIONS_DIRECTORY}`),
),
]);
const serializePaths = (paths: TPathsObject): TDirectory =>
const serializePaths = (paths: TPathsObject, parameters: Option<TParametersDefinitionsObject>): TDirectory =>
directory(
CONTROLLERS_DIRECTORY,
serializeDictionary(groupPathsByTag(paths), (name, group) =>
serializeDictionary(groupPathsByTag(paths, parameters), (name, group) =>
serializePathGroup(name, group, `${ROOT_DIRECTORY}/${CONTROLLERS_DIRECTORY}`),
),
);
Expand Down
55 changes: 49 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { array, uniq, flatten } from 'fp-ts/lib/Array';
import { array, uniq, flatten, last } from 'fp-ts/lib/Array';
import {
TPathItemObject,
TOperationObject,
Expand All @@ -9,12 +9,14 @@ import {
TReferenceObject,
TBodyParameterObject,
TSwaggerObject,
TParametersDefinitionsObject,
} from './swagger';
import { tuple } from 'fp-ts/lib/function';
import { setoidString } from 'fp-ts/lib/Setoid';
import { identity, tuple } from 'fp-ts/lib/function';
import { getRecordSetoid, setoidString } from 'fp-ts/lib/Setoid';
import { TQueryParameterObject } from './swagger';
import { TFSEntity } from './fs';
import { camelize } from '@devexperts/utils/dist/string/string';
import { Option, some } from 'fp-ts/lib/Option';

export type TSerializer = (name: string, schema: TSwaggerObject) => TFSEntity;

Expand All @@ -41,17 +43,58 @@ export const getTagsFromPath = (path: TPathItemObject): string[] => {
return uniq(setoidString)(tags);
};

export const groupPathsByTag = (paths: TPathsObject): TDictionary<TDictionary<TPathItemObject>> => {
const paramSetoid = getRecordSetoid<TParameterObject | TReferenceObject>({
name: setoidString,
$ref: setoidString,
});

export const resolveParam = (parameters: Option<TParametersDefinitionsObject>) => (
param: TParameterObject | TReferenceObject,
): Option<TParameterObject | TReferenceObject> => {
if (!isOperationReferenceParameterObject(param)) {
return some(param);
}
return last(param.$ref.split('/')).chain(ref => parameters.mapNullable(parameters => parameters[ref]));
};

export const addParamsToTag = (
params: Array<TParameterObject | TReferenceObject>,
parameters: Option<TParametersDefinitionsObject>,
) => (tag: TOperationObject): TOperationObject => ({
...tag,
parameters: tag.parameters
.alt(some([]))
.map(tagParameters =>
[...tagParameters, ...params].map(params => resolveParam(parameters)(params).getOrElse(params)),
)
.map(uniq(paramSetoid)),
});

export const groupPathsByTag = (
paths: TPathsObject,
parameters: Option<TParametersDefinitionsObject>,
): TDictionary<TDictionary<TPathItemObject>> => {
const keys = Object.keys(paths);
const result: TDictionary<TDictionary<TPathItemObject>> = {};
for (const key of keys) {
const path = paths[key];
const tags = getTagsFromPath(path);
const pathParams = path.parameters;
const addParams = pathParams.map(pathParams => addParamsToTag(pathParams, parameters)).getOrElse(identity);
const pathWithParams: TPathItemObject = pathParams
.map(() => ({
...path,
get: path.get.map(addParams),
post: path.post.map(addParams),
put: path.put.map(addParams),
delete: path.delete.map(addParams),
}))
.getOrElse(path);
const tags = getTagsFromPath(pathWithParams);
const tag = camelize(tags.join('').replace(/\s/g, ''), false);

result[tag] = {
...(result[tag] || {}),
[key]: path,
[key]: pathWithParams,
};
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ generate({
});

generate({
pathsToSpec: [path.resolve(self, './specs/yaml/swagger.yml'), path.resolve(self, './specs/yaml/common.yml')],
pathsToSpec: [path.resolve(self, './specs/yaml/common.yml'), path.resolve(self, './specs/yaml/swagger.yml')],
out: path.resolve(self, './out/yaml'),
serialize,
fileReader: fromYaml,
Expand Down
53 changes: 18 additions & 35 deletions test/specs/json/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@
"http"
],
"parameters": {

"petIdPathParam": {
"name": "petId",
"in": "path",
"description": "ID of pet to return",
"required": true,
"type": "integer",
"format": "int64"
}
},
"paths": {
"/pet": {
Expand Down Expand Up @@ -239,6 +246,11 @@
}
},
"/pet/{petId}": {
"parameters": [
{
"$ref": "#/parameters/petIdPathParam"
}
],
"get": {
"tags": [
"pet"
Expand All @@ -250,16 +262,6 @@
"application/xml",
"application/json"
],
"parameters": [
{
"name": "petId",
"in": "path",
"description": "ID of pet to return",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": {
"description": "successful operation",
Expand Down Expand Up @@ -295,14 +297,6 @@
"application/json"
],
"parameters": [
{
"name": "petId",
"in": "path",
"description": "ID of pet that needs to be updated",
"required": true,
"type": "integer",
"format": "int64"
},
{
"name": "name",
"in": "formData",
Expand Down Expand Up @@ -349,14 +343,6 @@
"in": "header",
"required": false,
"type": "string"
},
{
"name": "petId",
"in": "path",
"description": "Pet id to delete",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
Expand All @@ -378,6 +364,11 @@
}
},
"/pet/{petId}/uploadImage": {
"parameters": [
{
"$ref": "#/parameters/petIdPathParam"
}
],
"post": {
"tags": [
"pet"
Expand All @@ -392,14 +383,6 @@
"application/json"
],
"parameters": [
{
"name": "petId",
"in": "path",
"description": "ID of pet to update",
"required": true,
"type": "integer",
"format": "int64"
},
{
"name": "additionalMetadata",
"in": "formData",
Expand Down
3 changes: 1 addition & 2 deletions test/specs/yaml/common.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
swagger: '2.0'
info:
version: 1.0.0
title: Swagger Petstore
title: Common
paths: []

definitions:
Order:
type: object
Expand Down
Loading

0 comments on commit 9720eb0

Please sign in to comment.