Skip to content

Commit f35c553

Browse files
authored
1. routingTests.js runs through all operations described in openapi.yaml and tries calling them, expecting 200 in return. Currently not all tests pass - not supporting xml, and problems with formData (#3442)
2. Removed old testing files. 3. Added model files - contain data and structure as defined in openapi.yaml. Model.js has static methods relevant to all model files. 4. Changed openapi.yaml to allow running tests easily.
1 parent deebee4 commit f35c553

File tree

12 files changed

+295
-241
lines changed

12 files changed

+295
-241
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/NodeJSExpressServerCodegen.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public NodeJSExpressServerCodegen() {
9393
supportingFiles.add(new SupportingFile("utils" + File.separator + "writer.mustache", "utils", "writer.js"));
9494

9595
// controllers folder
96-
supportingFiles.add(new SupportingFile("controllers" + File.separator + "test.mustache", "controllers", "TestController.js"));
9796
supportingFiles.add(new SupportingFile("controllers" + File.separator + "index.mustache", "controllers", "index.js"));
9897
supportingFiles.add(new SupportingFile("controllers" + File.separator + "Controller.mustache", "controllers", "Controller.js"));
9998
// service folder

samples/server/petstore/nodejs-express-server/api/openapi.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ info:
88
title: OpenAPI Petstore
99
version: 1.0.0
1010
servers:
11-
- url: http://petstore.swagger.io/v2
11+
- url: http://localhost:{port}/api/{basePath}
12+
variables:
13+
port:
14+
enum:
15+
- '3000'
16+
- '3009'
17+
default: '3000'
18+
basePath:
19+
default: 'v2'
1220
tags:
1321
- description: Everything about your Pets
1422
name: pet

samples/server/petstore/nodejs-express-server/controllers/TestController.js

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
const PetController = require('./PetController');
22
const StoreController = require('./StoreController');
33
const UserController = require('./UserController');
4-
const TestController = require('./TestController');
54

65
module.exports = {
76
PetController,
87
StoreController,
98
UserController,
10-
TestController,
119
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const ono = require('ono');
2+
const Model = require('./Model');
3+
4+
class Category {
5+
constructor(name, id) {
6+
const validationErrors = (Model.validateModel(Category, {name, id}));
7+
if (validationErrors.length === 0) {
8+
this.id = id;
9+
this.name = name;
10+
} else {
11+
throw ono('Tried to create an invalid Category instance', {errors: validationErrors});
12+
}
13+
}
14+
}
15+
16+
Category.types = {
17+
id: 'integer',
18+
name: 'string',
19+
};
20+
21+
module.exports = Category;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Model {
2+
static validateModel(modelClass, variables) {
3+
const invalidArray = [];
4+
Object.entries(variables).forEach(([key, value]) => {
5+
const typeToCheck = modelClass.types[key];
6+
switch (typeToCheck) {
7+
case 'string':
8+
if (!(typeof value === 'string' || value instanceof String)) {
9+
invalidArray.push({ key, expectedType: typeToCheck, value });
10+
}
11+
break;
12+
case 'number':
13+
case 'integer':
14+
if (!(typeof value === 'number' && !Number.isNaN(value))) {
15+
invalidArray.push({ key, expectedType: typeToCheck, value });
16+
}
17+
break;
18+
case 'array':
19+
if (!(value && typeof value === 'object' && value.constructor === Array)) {
20+
invalidArray.push({ key, expectedType: typeToCheck, value });
21+
}
22+
break;
23+
case 'object':
24+
if (!(value && typeof value === 'object' && value.constructor === Array)) {
25+
invalidArray.push({ key, expectedType: typeToCheck, value });
26+
}
27+
break;
28+
case 'boolean':
29+
if (!(typeof value === 'boolean')) {
30+
invalidArray.push({ key, expectedType: typeToCheck, value });
31+
}
32+
break;
33+
default:
34+
break;
35+
}
36+
});
37+
modelClass.required.forEach((requiredFieldName) => {
38+
if (variables[requiredFieldName] === undefined || variables[requiredFieldName] === '') {
39+
invalidArray.push(
40+
{ field: requiredFieldName, required: true, value: variables[requiredFieldName] },
41+
);
42+
}
43+
});
44+
return invalidArray;
45+
}
46+
}
47+
module.exports = Model;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const ono = require('ono');
2+
const Model = require('./Model');
3+
const Tag = require('./Tag');
4+
const Category = require('./Category');
5+
6+
class Pet {
7+
constructor(photoUrls, name, id, tags, status, category) {
8+
const validationErrors = Model.validateModel(Pet, {
9+
photoUrls, name, id, tags, status, category,
10+
});
11+
if (validationErrors.length === 0) {
12+
this.photoUrls = photoUrls;
13+
this.name = name;
14+
this.id = id;
15+
this.tags = tags.map(t => new Tag(...t));
16+
this.status = status;
17+
this.category = new Category(category);
18+
} else {
19+
throw ono('Tried to create an invalid Pet instance', { errors: validationErrors });
20+
}
21+
}
22+
}
23+
24+
Pet.types = {
25+
photoUrls: 'array',
26+
name: 'string',
27+
id: 'string',
28+
tags: 'array',
29+
status: 'string',
30+
category: 'object',
31+
};
32+
33+
Pet.required = ['name', 'photoUrls'];
34+
35+
module.exports = Pet;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const ono = require('ono');
2+
const Model = require('./Model');
3+
4+
class Tag {
5+
constructor(name, id) {
6+
const validationErrors = Model.validateModel(Tag,
7+
{ name, id });
8+
if (validationErrors.length === 0) {
9+
this.name = name;
10+
this.id = id;
11+
} else {
12+
throw ono('Tried to create an invalid Tag instance', { errors: validationErrors });
13+
}
14+
}
15+
}
16+
17+
module.exports = Tag;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const CategoryModel = require('./Category');
2+
const PetModel = require('./Pet');
3+
const TagModel = require('./Tag');
4+
5+
module.exports = {
6+
CategoryModel,
7+
PetModel,
8+
TagModel,
9+
};

samples/server/petstore/nodejs-express-server/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"start": "node index.js"
99
},
1010
"keywords": [
11-
"openapi-generator", "openapi"
11+
"openapi-generator",
12+
"openapi"
1213
],
1314
"license": "Unlicense",
1415
"private": true,
@@ -20,6 +21,9 @@
2021
"express": "^4.16.4",
2122
"express-openapi-validator": "^1.0.0",
2223
"js-yaml": "^3.3.0",
24+
"jstoxml": "^1.5.0",
25+
"ono": "^5.0.1",
26+
"openapi-sampler": "^1.0.0-beta.15",
2327
"swagger-express-middleware": "^2.0.2",
2428
"swagger-tools": "^0.10.4",
2529
"swagger-ui-express": "^4.0.2",
@@ -35,5 +39,10 @@
3539
"eslint-plugin-import": "^2.17.2",
3640
"form-data": "^2.3.3",
3741
"mocha": "^6.1.4"
42+
},
43+
"eslintConfig": {
44+
"env": {
45+
"node": true
46+
}
3847
}
3948
}

0 commit comments

Comments
 (0)