Skip to content
This repository was archived by the owner on Dec 10, 2021. It is now read-only.

make it work with swagger 2.0 syntax #40

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 deletions src/DefinitionGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class DefinitionGenerator {
}

this.definition.components.schemas = await parseModels(models, this.root);

this.config.models = _.values(this.definition.components.schemas);
return this;
}

Expand Down Expand Up @@ -97,7 +97,7 @@ export class DefinitionGenerator {
// loop through function configurations
for (const funcConfig of config) {
// loop through http events
for (const httpEvent of this.getHttpEvents(funcConfig.events)) {
for (const httpEvent of this.getHttpEvents(funcConfig.events || [])) {
const httpEventConfig = httpEvent.http;

if (httpEventConfig.documentation) {
Expand Down Expand Up @@ -352,7 +352,7 @@ export class DefinitionGenerator {
return responses;
}

private getResponseContent(response) {
private getResponseContent(response ={}) {
const content = {};

for (const responseKey of Object.keys(response)) {
Expand Down
104 changes: 104 additions & 0 deletions src/__tests__/DefinitionGeneratorSwagger2Syntax.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import _ = require("lodash");
import * as path from "path";
import * as Serverless from "serverless";
import { DefinitionGenerator } from "../DefinitionGenerator";

class ServerlessInterface extends Serverless {
public service: any = {};
public config: any = {};
public yamlParser: any = {};
public pluginManager: any = {};
public variables: any = {};
}

describe("OpenAPI Documentation Generator", () => {
let sls: ServerlessInterface;

const servicePath = path.join(__dirname, "../../test/project-swagger-2.0");

beforeEach(async () => {
const serverlessYamlPath = path.join(servicePath, "./serverless.yml");
sls = new Serverless();

sls.config.update({
servicePath
});

const config = await sls.yamlParser.parse(serverlessYamlPath);
sls.pluginManager.cliOptions = { stage: "dev" };

await sls.service.load(config);
await sls.variables.populateService();

if (!("documentation" in sls.service.custom)) {
throw new Error(
'Cannot find "documentation" in custom section of "serverless.yml"'
);
}
});


it("resolves the DTOs recursively using the swagger 2.0 spec : $ref: {{model: OtherDTO}}", async () => {
const docGen = new DefinitionGenerator(
sls.service.custom.documentation,
servicePath
);

// implementation copied from ServerlessOpenApiDocumentation.ts
await docGen.parse();

const funcConfigs = sls.service.getAllFunctions().map(functionName => {
const func = sls.service.getFunction(functionName);
return _.merge({ _functionName: functionName }, func);
});

docGen.readFunctions(funcConfigs);


let expected = {
"CompaniesDTO": {
"properties": {
"companies": {
"items": {
"$ref": "#/components/schemas/CompanyDTO"
},
"type": "array"
}
},
"type": "object"
},
"CompanyDTO": {
"properties": {
"client": {
"$ref": "#/components/schemas/UserDTO"
},
"name": {
"type": "string"
},
"notes": {
"type": "string"
}
},
"type": "object"
},
"UserDTO": {
"properties": {
"companyName": {
"type": "string"
},
"email": {
"type": "string"
},
"firstName": {
"type": "string"
},
"surname": {
"type": "string"
}
},
"type": "object"
}
};
expect(docGen.definition.components.schemas).toEqual(expected)
});
});
5 changes: 4 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ function updateReferences(schema: JSONSchema7): JSONSchema7 {
const cloned = _.cloneDeep(schema);

if (cloned.$ref) {
let referencedValue = cloned.$ref
.replace("#/definitions", "#/components/schemas") // json schema syntax
.replace(/{{model: (\w+)}}/, "#/components/schemas/$1"); // swagger 2.0 syntax
return {
...cloned,
$ref: cloned.$ref.replace("#/definitions", "#/components/schemas")
$ref: referencedValue
};
}

Expand Down