Skip to content

Commit

Permalink
FE: fixed fe of field-level default value of enums in internal defini…
Browse files Browse the repository at this point in the history
…tions (#157)
  • Loading branch information
Vitalii4as committed Mar 27, 2024
1 parent a03bbda commit dca4049
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 3 additions & 2 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,14 @@ const convertSchemaToUserDefinedTypes = definitionsSchema => {
return {
name: prepareName(key),
schema: convertSchema(definition),
originalSchema: definition,
customProperties,
}
});

return definitions.reduce((result, { name, schema, customProperties }) => ({
return definitions.reduce((result, { name, schema, customProperties, originalSchema }) => ({
...result,
[name]: { schema, customProperties },
[name]: { schema, customProperties, originalSchema },
}), {});
};

Expand Down
19 changes: 16 additions & 3 deletions forward_engineering/helpers/convertJsonSchemaToAvro.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const handleRequired = schema => {

return {
...result,
[key]: _.omit(property, 'default'),
[key]: { ..._.omit(property, 'default'), required: true },
};
}, {}),
};
Expand Down Expand Up @@ -302,22 +302,34 @@ const convertArray = schema => {
};

const handleField = (name, field) => {
const { description, refDescription, default: defaultValue, order, aliases, ...schema } = field;
const { description, refDescription, default: __defaultValue, order, aliases, ...schema } = field;
const typeSchema = convertSchema(schema);
const udt = getUdtItem(typeSchema);
const customProperties = udt?.customProperties || getCustomProperties(getFieldLevelConfig(schema.type), schema);

return resolveFieldDefaultValue({
name: prepareName(name),
type: _.isArray(typeSchema.type) ? typeSchema.type : typeSchema,
default: !_.isUndefined(defaultValue) || typeSchema?.type === 'enum' ? defaultValue : typeSchema?.default,
default: getEnumDefaultValue(field, udt, typeSchema),
doc: field.$ref ? refDescription : description,
order,
aliases,
...customProperties,
}, typeSchema);
};

const getEnumDefaultValue = (field, udt, typeSchema) => {
if (!_.isUndefined(field?.default) || typeSchema?.type === 'enum') {
return field?.default;
}

if (field?.$ref && !field?.required && udt?.originalSchema?.type === 'enum') {
return udt?.originalSchema?.default;
}

return typeSchema?.default;
}

const resolveFieldDefaultValue = (field, type) => {
let udtItem = _.isString(type) && getUdtItem(type);

Expand Down Expand Up @@ -392,6 +404,7 @@ const convertNamedType = (schema, schemaTypeKeysMap = {}) => {
addDefinitions({ [name]: {
schema: { ...filterSchemaAttributes(schema), ..._.pick(schema, schemaTypeSpecificKeys) },
customProperties: getCustomProperties(getFieldLevelConfig(schema.type)),
originalSchema: schema,
used: true,
}});
}
Expand Down
1 change: 1 addition & 0 deletions forward_engineering/helpers/udtHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ const convertCollectionReferences = (entities, options) => {
[reference.name]: {
isCollectionReference: true,
schema: {},
originalSchema: {},
}
}), {}));

Expand Down

0 comments on commit dca4049

Please sign in to comment.