Skip to content

Commit 95aad21

Browse files
committed
default tag
1 parent f23dbb6 commit 95aad21

File tree

4 files changed

+130
-10
lines changed

4 files changed

+130
-10
lines changed

src/N8NPropertiesBuilder.spec.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,3 +938,120 @@ test('multiple tags', () => {
938938
]
939939
);
940940
});
941+
942+
test('no tags - default tag', () => {
943+
const paths = {
944+
'/api/entities': {
945+
get: {
946+
operationId: 'EntityController_list',
947+
summary: 'List all entities',
948+
parameters: [
949+
{
950+
name: 'all',
951+
required: false,
952+
in: 'query',
953+
example: false,
954+
description: 'Boolean flag description',
955+
schema: {
956+
type: 'boolean',
957+
},
958+
},
959+
],
960+
tags: [],
961+
},
962+
},
963+
};
964+
965+
const parser = new N8NPropertiesBuilder({paths});
966+
const result = parser.build()
967+
968+
expect(result).toEqual(
969+
[
970+
{
971+
"default": "",
972+
"displayName": "Resource",
973+
"name": "resource",
974+
"noDataExpression": true,
975+
"options": [
976+
{
977+
"description": "",
978+
"name": "Default",
979+
"value": "Default"
980+
}
981+
],
982+
"type": "options"
983+
},
984+
{
985+
"default": "",
986+
"displayName": "Operation",
987+
"displayOptions": {
988+
"show": {
989+
"resource": [
990+
"Default"
991+
]
992+
}
993+
},
994+
"name": "operation",
995+
"noDataExpression": true,
996+
"options": [
997+
{
998+
"action": "List all entities",
999+
"description": "List all entities",
1000+
"name": "List",
1001+
"routing": {
1002+
"request": {
1003+
"method": "GET",
1004+
"url": "=/api/entities"
1005+
}
1006+
},
1007+
"value": "List"
1008+
}
1009+
],
1010+
"type": "options"
1011+
},
1012+
{
1013+
"default": "",
1014+
"displayName": "GET /api/entities",
1015+
"displayOptions": {
1016+
"show": {
1017+
"operation": [
1018+
"List"
1019+
],
1020+
"resource": [
1021+
"Default"
1022+
]
1023+
}
1024+
},
1025+
"name": "operation",
1026+
"type": "notice",
1027+
"typeOptions": {
1028+
"theme": "info"
1029+
}
1030+
},
1031+
{
1032+
"default": false,
1033+
"description": "Boolean flag description",
1034+
"displayName": "All",
1035+
"displayOptions": {
1036+
"show": {
1037+
"operation": [
1038+
"List"
1039+
],
1040+
"resource": [
1041+
"Default"
1042+
]
1043+
}
1044+
},
1045+
"name": "all",
1046+
"routing": {
1047+
"request": {
1048+
"qs": {
1049+
"all": "={{ $value }}"
1050+
}
1051+
}
1052+
},
1053+
"type": "boolean"
1054+
}
1055+
]
1056+
);
1057+
});

src/OperationParser.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ export interface IOperationParser {
1515

1616
export class N8NOperationParser implements IOperationParser {
1717
getResources(operation: OpenAPIV3.OperationObject, context: OperationContext): string[] {
18-
const tags = operation.tags;
19-
if (!tags || tags.length === 0) {
20-
// TODO: Add "default" tag
21-
throw new Error(`No tags found for operation '${operation}'`);
22-
}
18+
const tags = operation.tags as string[]
2319
return tags.map(toResourceName)
2420
}
2521

src/ResourcePropertiesCollector.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,19 @@ export class ResourcePropertiesCollector implements OpenAPIVisitor {
4545
tags.sort((a, b) => {
4646
return this.tagsOrder.get(a.name,)! - this.tagsOrder.get(b.name)!;
4747
})
48+
// put "default" at the end if not present explicitly in 'tags"
49+
if (!this.tagsOrder.has('default')) {
50+
const defaultTag = tags.find((tag) => tag.name === 'default')
51+
if (defaultTag) {
52+
tags.splice(tags.indexOf(defaultTag), 1)
53+
tags.push(defaultTag)
54+
}
55+
}
4856
return tags;
4957
}
5058

5159
visitOperation(operation: OpenAPIV3.OperationObject, context: OperationContext) {
52-
let tags = operation.tags
53-
if (!tags || tags.length === 0) {
54-
// TODO: add 'default' at the end
55-
return;
56-
}
60+
let tags = operation.tags as string[]
5761
tags.forEach((tag) => this.addTagByName(tag))
5862
}
5963

src/openapi/OpenAPIWalker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class OpenAPIWalker {
4444
if (!HttpMethods.includes(method)) {
4545
continue;
4646
}
47+
if (!operation.tags || operation.tags.length === 0) {
48+
operation.tags = ['default']
49+
}
4750
if (operation && visitor.visitOperation) {
4851
const context = {pattern: path, path: pathItem, method: method as OpenAPIV3.HttpMethods};
4952
visitor.visitOperation(operation, context);

0 commit comments

Comments
 (0)