From df4cf50b30ff17ac64262e73311d4bbe45a1e702 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Fri, 22 Mar 2024 17:19:19 +0100 Subject: [PATCH 1/2] :white_check_mark: [#4038] Add tests for variable/component -> json schema translation --- .../registrations/objectsapi/utils.spec.js | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/openforms/js/components/admin/form_design/registrations/objectsapi/utils.spec.js diff --git a/src/openforms/js/components/admin/form_design/registrations/objectsapi/utils.spec.js b/src/openforms/js/components/admin/form_design/registrations/objectsapi/utils.spec.js new file mode 100644 index 0000000000..8c0a029890 --- /dev/null +++ b/src/openforms/js/components/admin/form_design/registrations/objectsapi/utils.spec.js @@ -0,0 +1,77 @@ +import {EMPTY_VARIABLE} from 'components/admin/form_design/variables/constants'; + +import {asJsonSchema} from './utils'; + +test('JSON schema for single file upload component', () => { + const component = { + type: 'file', + key: 'file', + label: 'File', + multiple: false, + }; + const variable = { + ...EMPTY_VARIABLE, + source: 'component', + key: 'file', + dataType: 'array', + }; + + const schema = asJsonSchema(variable, {[component.key]: component}); + + expect(schema).toEqual({ + type: 'string', + format: 'uri', + }); +}); + +test('JSON schema for multiple file uploads component', () => { + const component = { + type: 'file', + key: 'file', + label: 'File', + multiple: true, + }; + const variable = { + ...EMPTY_VARIABLE, + source: 'component', + key: 'file', + dataType: 'array', + }; + + const schema = asJsonSchema(variable, {[component.key]: component}); + + expect(schema).toEqual({ + type: 'array', + items: { + type: 'string', + format: 'uri', + }, + }); +}); + +test('JSON schema for map components', () => { + const component = { + type: 'map', + key: 'map', + label: 'Point coordinates', + }; + const variable = { + ...EMPTY_VARIABLE, + source: 'component', + key: 'map', + dataType: 'array', + }; + + const schema = asJsonSchema(variable, {[component.key]: component}); + + // we could be stricter, but we basically expect GeoJSON at the other end, which is + // quite complex. + expect(schema).toEqual({ + type: 'object', + properties: { + type: { + type: 'string', + }, + }, + }); +}); From 60bad42f8e9aa9d7693a313ea94d50f2a6ad6b69 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Fri, 22 Mar 2024 17:19:29 +0100 Subject: [PATCH 2/2] :sparkles: [#4038] Implement translation from map component -> JSON schema --- .../registrations/objectsapi/utils.js | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/openforms/js/components/admin/form_design/registrations/objectsapi/utils.js b/src/openforms/js/components/admin/form_design/registrations/objectsapi/utils.js index 1271f52538..a0d2852110 100644 --- a/src/openforms/js/components/admin/form_design/registrations/objectsapi/utils.js +++ b/src/openforms/js/components/admin/form_design/registrations/objectsapi/utils.js @@ -53,16 +53,34 @@ const FORMAT_TYPE_MAP = { * @returns {Object} - The JSON Schema */ const asJsonSchema = (variable, components) => { - // Figure out if the component is a file component (special case) - const componentDefinition = components[variable.key]; - if (componentDefinition && componentDefinition.type === 'file') { - // If it is, and it has multiple == True, then type is array - if (componentDefinition.multiple) - return {type: 'array', items: {type: 'string', format: 'uri'}}; - // Otherwise it's string (URL of the document) - return {type: 'string', format: 'uri'}; + // Special handling for component types. + if (variable.source === 'component') { + const componentDefinition = components[variable.key]; + + switch (componentDefinition.type) { + case 'file': { + const {multiple = false} = componentDefinition; + const uriSchema = {type: 'string', format: 'uri'}; + // If the component allows multiple files, the type is array, otherwise it's + // just a single string. + return multiple ? {type: 'array', items: uriSchema} : uriSchema; + } + case 'map': { + // TODO: in the future we could use `const: 'Point'` for the `type` key to be + // more strict. + return { + type: 'object', + properties: { + type: { + type: 'string', + }, + }, + }; + } + } } + // default behaviour - map the variable data type to a json-schema type. if (VARIABLE_TYPE_MAP.hasOwnProperty(variable.dataType)) return {type: VARIABLE_TYPE_MAP[variable.dataType]}; return {