diff --git a/forward_engineering/api.js b/forward_engineering/api.js index 69b9009..a9b5615 100644 --- a/forward_engineering/api.js +++ b/forward_engineering/api.js @@ -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 }, }), {}); }; diff --git a/forward_engineering/helpers/convertJsonSchemaToAvro.js b/forward_engineering/helpers/convertJsonSchemaToAvro.js index e139b81..7b4744c 100644 --- a/forward_engineering/helpers/convertJsonSchemaToAvro.js +++ b/forward_engineering/helpers/convertJsonSchemaToAvro.js @@ -137,7 +137,7 @@ const handleRequired = schema => { return { ...result, - [key]: _.omit(property, 'default'), + [key]: { ..._.omit(property, 'default'), required: true }, }; }, {}), }; @@ -302,7 +302,7 @@ 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); @@ -310,7 +310,7 @@ const handleField = (name, field) => { 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, @@ -318,6 +318,18 @@ const handleField = (name, field) => { }, 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); @@ -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, }}); } diff --git a/forward_engineering/helpers/udtHelper.js b/forward_engineering/helpers/udtHelper.js index 11ede07..01a726e 100644 --- a/forward_engineering/helpers/udtHelper.js +++ b/forward_engineering/helpers/udtHelper.js @@ -231,6 +231,7 @@ const convertCollectionReferences = (entities, options) => { [reference.name]: { isCollectionReference: true, schema: {}, + originalSchema: {}, } }), {}));