Skip to content
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

FE: fixed fe of field-level default value of enums in internal definitions #157

Merged
merged 1 commit into from
Mar 27, 2024
Merged
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
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