Skip to content

Transform array of enums in multi-options #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 25 additions & 4 deletions src/n8n/SchemaToINodeProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {INodeProperties, NodePropertyTypes} from "n8n-workflow";
import {RefResolver} from "../openapi/RefResolver";
import * as lodash from "lodash";
import {SchemaExample} from "../openapi/SchemaExample";
import {
INodePropertyCollection,
INodePropertyOptions
} from 'n8n-workflow/dist/Interfaces'

type Schema = OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
type FromSchemaNodeProperty = Pick<INodeProperties, 'type' | 'default' | 'description' | 'options'>;
Expand Down Expand Up @@ -43,6 +47,8 @@ export class N8NINodeProperties {
let type: NodePropertyTypes;
let defaultValue = this.schemaExample.extractExample(schema)

let options: Array<INodePropertyOptions | INodeProperties | INodePropertyCollection> | undefined = undefined

switch (schema.type) {
case 'boolean':
type = 'boolean';
Expand All @@ -58,8 +64,20 @@ export class N8NINodeProperties {
defaultValue = defaultValue !== undefined ? JSON.stringify(defaultValue, null, 2) : '{}';
break;
case 'array':
type = 'json';
defaultValue = defaultValue !== undefined ? JSON.stringify(defaultValue, null, 2) : '[]';
let schemaAsArray = schema as any;
if (schemaAsArray.items && schemaAsArray.items.enum && schemaAsArray.items.enum.length > 0) {
type = 'multiOptions';
options = schemaAsArray.items.enum.map((value: string) => {
return {
name: lodash.startCase(value),
value: value,
};
});
defaultValue = defaultValue !== undefined ? defaultValue : [];
} else {
type = 'json';
defaultValue = defaultValue !== undefined ? JSON.stringify(defaultValue, null, 2) : '[]';
}
break;
case 'number':
case 'integer':
Expand All @@ -68,11 +86,14 @@ export class N8NINodeProperties {
break;
}

const field: FromSchemaNodeProperty = {
let field: FromSchemaNodeProperty = {
type: type,
default: defaultValue,
description: schema.description,
};
if (options) {
field.options = options;
}
if (schema.enum && schema.enum.length > 0) {
field.type = 'options';
field.options = schema.enum.map((value: string) => {
Expand Down Expand Up @@ -115,7 +136,7 @@ export class N8NINodeProperties {
send: {
type: 'query',
property: parameter.name,
value: '={{ $value }}',
value: fieldSchemaKeys.type === "multiOptions" && !parameter.explode ? "={{ $value.join(',') }}" : '={{ $value }}',
propertyInDotNotation: false,
},
};
Expand Down
131 changes: 131 additions & 0 deletions tests/multioptions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {N8NPropertiesBuilder} from "../src/N8NPropertiesBuilder";
import {INodeProperties} from "n8n-workflow";

test('petstore.json', () => {
const doc = require('./samples/multioptions.json');
const config = {}
const parser = new N8NPropertiesBuilder(doc, config);
const result = parser.build()

console.log(JSON.stringify(result, null, 2))
const expected: INodeProperties[] = [
{
"displayName": "Resource",
"name": "resource",
"type": "options",
"noDataExpression": true,
"options": [
{
"name": "Default",
"value": "Default",
"description": ""
}
],
"default": ""
},
{
"displayName": "Operation",
"name": "operation",
"type": "options",
"noDataExpression": true,
"displayOptions": {
"show": {
"resource": [
"Default"
]
}
},
"options": [
{
"name": "Create Model Fields Only",
"value": "Create Model Fields Only",
"action": "Create a model with field selection",
"description": "Create a model with field selection",
"routing": {
"request": {
"method": "POST",
"url": "=/model/"
}
}
}
],
"default": ""
},
{
"displayName": "POST /model/",
"name": "operation",
"type": "notice",
"typeOptions": {
"theme": "info"
},
"default": "",
"displayOptions": {
"show": {
"resource": [
"Default"
],
"operation": [
"Create Model Fields Only"
]
}
}
},
{
"displayName": "Fields Model",
"name": "fields%5Bmodel%5D",
"description": "Specifies which fields should be returned.",
"default": [],
"type": "multiOptions",
"options": [
{
"name": "Component Type",
"value": "componentType"
},
{
"name": "Count",
"value": "count"
},
{
"name": "Array",
"value": "array"
},
{
"name": "Model Type",
"value": "modelType"
},
{
"name": "Sub Model",
"value": "subModel"
},
{
"name": "View",
"value": "view"
},
{
"name": "Name",
"value": "name"
}
],
"routing": {
"send": {
"type": "query",
"property": "fields[model]",
"value": "={{ $value.join(',') }}",
"propertyInDotNotation": false
}
},
"displayOptions": {
"show": {
"resource": [
"Default"
],
"operation": [
"Create Model Fields Only"
]
}
}
}
]
;
expect(result).toEqual(expected);
})
44 changes: 44 additions & 0 deletions tests/samples/multioptions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"openapi": "3.1.0",
"info": {
"title": "Multi Option Example",
"version": "0.1.0"
},
"paths": {
"/model/": {
"post": {
"summary": "Create a model with field selection",
"operationId": "create_model_fields_only",
"parameters": [
{
"name": "fields[model]",
"in": "query",
"description": "Specifies which fields should be returned.",
"required": false,
"schema": {
"type": "array",
"items": {
"type": "string",
"enum": [
"componentType",
"count",
"array",
"modelType",
"subModel",
"view",
"name"
]
}
},
"explode": false
}
],
"responses": {
"200": {
"description": "Request accepted with specified fields."
}
}
}
}
}
}