From 2a88c4b3fc7e1ee6cf722a6c6dc8c6661e7af070 Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Sat, 25 May 2024 14:24:20 +0200 Subject: [PATCH] Handle parameters with multiple schemas of similar types correctly, handle `additionalProperties: false` correctly in ObjectEditor --- src/components/ParameterDataTypes.vue | 47 +++++++++++++------ .../datatypes/FileFormatOptionsEditor.vue | 13 +++-- src/components/datatypes/ObjectEditor.vue | 7 ++- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/components/ParameterDataTypes.vue b/src/components/ParameterDataTypes.vue index 1fea66aad..2728ab1f9 100644 --- a/src/components/ParameterDataTypes.vue +++ b/src/components/ParameterDataTypes.vue @@ -4,11 +4,11 @@ @@ -225,7 +225,7 @@ export default { } else { for(let type of this.parameter.schemas) { - let name = type.dataType(); + const name = this.getUniqueKey(allowed, type.dataType()); allowed[name] = type; } } @@ -238,8 +238,8 @@ export default { if (s.any === false) { continue; } - let name = s.subtype || s.type; - let schema = Object.assign({}, API_TYPES[name], s); + const name = s.subtype || s.type; + const schema = Object.assign({}, API_TYPES[name], s); map[name] = new ProcessDataType(schema, this.parameter); } return map; @@ -249,12 +249,10 @@ export default { for(let type in this.allowedTypes) { let schema = this.allowedTypes[type]; let group = schema.group(); - if (!Array.isArray(grouped[group])) { - grouped[group] = [schema]; - } - else { - grouped[group].push(schema); + if (!Utils.isObject(grouped[group])) { + grouped[group] = {}; } + grouped[group][type] = schema; } let groups = TYPE_GROUPS .map(group => ({ @@ -263,6 +261,16 @@ export default { })) .filter(group => group.types.length !== 0); return groups; + }, + detectableTypes() { + const detectable = {}; + for(let key in this.allowedTypes) { + let type = this.allowedTypes[key]; + if (!type.schema.noAutoDetect) { + detectable[key] = type; + } + } + return detectable; } }, watch: { @@ -290,6 +298,15 @@ export default { } }, methods: { + getUniqueKey(obj, basename) { + let name = basename; + let index = 2; + while (obj[name]) { + name = basename + String(index); + index++; + } + return name; + }, async isValueInvalid(value, schema) { let schema2 = Utils.deepClone(schema); // Allow from_node and from_parameter in values, see https://github.com/Open-EO/openeo-web-editor/issues/179 @@ -312,13 +329,14 @@ export default { * @return {string[]} - Returns matching indices (as strings!). */ async getTypeForValue(types, value) { - var validTypes = []; - for(var type of types) { + const validTypes = []; + for(let key in types) { + let type = types[key]; try { if (await this.isValueInvalid(value, type.schema)) { continue; } - validTypes.push(type.dataType()); + validTypes.push(key); } catch (error) {} } return validTypes; @@ -342,8 +360,7 @@ export default { } } else { - let detectableTypes = Object.values(this.allowedTypes).filter(type => !type.schema.noAutoDetect); - let types = await this.getTypeForValue(detectableTypes, this.state); + let types = await this.getTypeForValue(this.detectableTypes, this.state); if (types.length === 0) { await this.setSelected('json'); } diff --git a/src/components/datatypes/FileFormatOptionsEditor.vue b/src/components/datatypes/FileFormatOptionsEditor.vue index 7379bbea8..49e2c3342 100644 --- a/src/components/datatypes/FileFormatOptionsEditor.vue +++ b/src/components/datatypes/FileFormatOptionsEditor.vue @@ -21,7 +21,7 @@ import ParameterDataTypes from '../ParameterDataTypes.vue'; import Utils from '../../utils.js'; import Description from '@openeo/vue-components/components/Description.vue'; -import { ProcessParameter } from '@openeo/js-commons'; +import { ProcessParameter, ProcessDataType } from '@openeo/js-commons'; export default { name: 'FileFormatOptionsEditor', @@ -69,16 +69,15 @@ export default { schema.examples = [schema.example]; delete schema.example; } - parameters.push(new ProcessParameter({ + const parameter = new ProcessParameter({ name: name, description: schema.description, - schema: [ - {subtype: 'undefined', not: {}}, - schema - ], + schema, optional: !schema.required, default: schema.default - })); + }); + parameter.schemas.push(new ProcessDataType({subtype: 'undefined', not: {}}, parameter)); + parameters.push(parameter); } return parameters; }, diff --git a/src/components/datatypes/ObjectEditor.vue b/src/components/datatypes/ObjectEditor.vue index 0a08c8ea3..065814dff 100644 --- a/src/components/datatypes/ObjectEditor.vue +++ b/src/components/datatypes/ObjectEditor.vue @@ -1,8 +1,8 @@