Skip to content

Commit

Permalink
Fix/hck 4778 hackolade assignment examples are always genera (#68)
Browse files Browse the repository at this point in the history
* field-level-config: change property type for example to dynamic field

* field-level-config: split example property config by version

* FE: add change type of parameter example value according to schema type

* adapter: add adapter to re-validate sample values

* FE: move correct type method to type helper

* Revert "adapter: add adapter to re-validate sample values"

This reverts commit c041231.

* FE: move method into separate file

* FE: fix import circular dependency
  • Loading branch information
serhii-filonenko authored Mar 21, 2024
1 parent e02d369 commit 24419be
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 18 deletions.
45 changes: 45 additions & 0 deletions forward_engineering/helpers/componentsHelpers/exampleDataHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function parseExample(data) {
try {
return JSON.parse(data);
} catch (err) {
return data;
}
}

function parseExampleValueByDataType(value, type) {
const parsedValue = parseExample(value);

switch (type) {
case 'string':
if (typeof parsedValue === 'string') {
return parsedValue;
}
break;
case 'number':
case 'integer':
if (!isNaN(parsedValue)) {
return parsedValue;
}
break;
case 'array':
if (Array.isArray(parsedValue)) {
return parsedValue;
}
break;
case 'object':
if (typeof parsedValue === 'object' && parsedValue !== null) {
return parsedValue;
}
break;
case 'boolean':
if (typeof parsedValue === 'boolean') {
return parsedValue;
}
}

return value;
}

module.exports = {
parseExampleValueByDataType
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { getExamples } = require('./examplesHelper');
const { getRef, hasRef, hasChoice } = require('../typeHelper');
const { commentDeactivatedItemInner } = require('../commentsHelper');
const { activateItem } = require('../commonHelper');
const { parseExampleValueByDataType } = require('./exampleDataHelper');

function getParameters(data, specVersion) {
if (!data || !data.properties) {
Expand All @@ -31,6 +32,9 @@ function mapParameter({ data, required, isParentActivated = false, specVersion})
}
const schemaKeyword = getSchemaKeyword(data.properties);
const isActivated = data.isActivated && isParentActivated;
const schema = mapSchema({ data: get(data, ['properties', schemaKeyword]), key: 'schema', isParentActivated: isActivated, specVersion });
const example = parseExampleValueByDataType(data.sample, schema.type);

const parameter = {
name: data.parameterName,
in: getIn(data.type),
Expand All @@ -41,8 +45,8 @@ function mapParameter({ data, required, isParentActivated = false, specVersion})
style: data.style,
explode: data.explode,
allowReserved: data.allowReserved,
schema: mapSchema({ data: get(data, ['properties', schemaKeyword]), key: 'schema', isParentActivated: isActivated, specVersion }),
example: data.sample,
schema,
example,
examples: getExamples(get(data, 'properties.examples'), specVersion),
content: getContent({ data: get(data, 'properties.content'), isParentActivated: isActivated, specVersion })
};
Expand Down
6 changes: 4 additions & 2 deletions forward_engineering/helpers/typeHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const get = require('lodash.get');
const getExtensions = require('./extensionsHelper');
const { commentDeactivatedItemInner } = require('./commentsHelper');
const { isTargetVersionJSONSchemaCompatible, getArrayItems } = require('./sharedHelper');
const { parseExampleValueByDataType } = require('./componentsHelpers/exampleDataHelper');

const CONDITIONAL_ITEM_NAME = '<conditional>';

Expand Down Expand Up @@ -191,7 +192,7 @@ function getPrimitiveTypeProps(data, specVersion) {
xml: getXml(data.xml),
readOnly: data.readOnly || undefined,
writeOnly: data.writeOnly || undefined,
example: data.sample,
example: parseExampleValueByDataType(data.sample, data.type),
examples: data.examples,
...getExtensions(data.scopesExtensions)
};
Expand Down Expand Up @@ -350,5 +351,6 @@ module.exports = {
getType,
getRef,
hasRef,
hasChoice
hasChoice,
parseExample,
};
193 changes: 179 additions & 14 deletions properties_pane/field_level/fieldLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,27 @@ making sure that you maintain a proper JSON format.
{
"propertyName": "example (deprecated)",
"propertyKeyword": "sample",
"propertyType": "text",
"shouldValidate": true
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}
},
{
"propertyName": "example",
"propertyKeyword": "sample",
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"type": "not",
"values": [{
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}]
}
},
{
"propertyName": "nullable",
Expand Down Expand Up @@ -683,8 +702,27 @@ making sure that you maintain a proper JSON format.
{
"propertyName": "example (deprecated)",
"propertyKeyword": "sample",
"propertyType": "text",
"shouldValidate": true
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}
},
{
"propertyName": "example",
"propertyKeyword": "sample",
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"type": "not",
"values": [{
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}]
}
},
{
"propertyName": "nullable",
Expand Down Expand Up @@ -994,8 +1032,27 @@ making sure that you maintain a proper JSON format.
{
"propertyName": "example (deprecated)",
"propertyKeyword": "sample",
"propertyType": "text",
"shouldValidate": true
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}
},
{
"propertyName": "example",
"propertyKeyword": "sample",
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"type": "not",
"values": [{
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}]
}
},
{
"propertyName": "nullable",
Expand Down Expand Up @@ -1364,8 +1421,27 @@ making sure that you maintain a proper JSON format.
{
"propertyName": "example (deprecated)",
"propertyKeyword": "sample",
"propertyType": "text",
"shouldValidate": true
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}
},
{
"propertyName": "example",
"propertyKeyword": "sample",
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"type": "not",
"values": [{
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}]
}
},
{
"propertyName": "nullable",
Expand Down Expand Up @@ -1532,8 +1608,27 @@ making sure that you maintain a proper JSON format.
{
"propertyName": "example (deprecated)",
"propertyKeyword": "sample",
"propertyType": "text",
"shouldValidate": true
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}
},
{
"propertyName": "example",
"propertyKeyword": "sample",
"propertyType": "dynamicField",
"shouldValidate": true,
"dependency": {
"type": "not",
"values": [{
"level":"model",
"key": "dbVersion",
"value": "3.1.0"
}]
}
},
"comments",
{
Expand Down Expand Up @@ -1796,8 +1891,43 @@ making sure that you maintain a proper JSON format.
"propertyType": "details",
"template": "textAreaJson",
"dependency": {
"key": "subtype",
"value": "schema"
"type": "and",
"values": [
{
"key": "subtype",
"value": "schema"
},
{
"level": "model",
"key": "dbVersion",
"value": "3.1.0"
}
]
}
},
{
"propertyName": "example",
"propertyKeyword": "sample",
"propertyType": "details",
"template": "textAreaJson",
"dependency": {
"type": "and",
"values": [
{
"key": "subtype",
"value": "schema"
},
{
"type": "not",
"values": [
{
"level": "model",
"key": "dbVersion",
"value": "3.1.0"
}
]
}
]
}
},
"comments",
Expand Down Expand Up @@ -2207,8 +2337,43 @@ making sure that you maintain a proper JSON format.
"propertyType": "details",
"template": "textAreaJson",
"dependency": {
"key": "subtype",
"value": "schema"
"type": "and",
"values": [
{
"key": "subtype",
"value": "schema"
},
{
"level": "model",
"key": "dbVersion",
"value": "3.1.0"
}
]
}
},
{
"propertyName": "example",
"propertyKeyword": "sample",
"propertyType": "details",
"template": "textAreaJson",
"dependency": {
"type": "and",
"values": [
{
"key": "subtype",
"value": "schema"
},
{
"type": "not",
"values": [
{
"level": "model",
"key": "dbVersion",
"value": "3.1.0"
}
]
}
]
}
},
{
Expand Down

0 comments on commit 24419be

Please sign in to comment.