@@ -12,6 +12,7 @@ const {
1212 convertCollectionReferences,
1313 resolveNamespaceReferences,
1414 clearDefinitions,
15+ resolveSchemaUdt,
1516} = require ( './helpers/udtHelper' ) ;
1617const convertSchema = require ( './helpers/convertJsonSchemaToAvro' ) ;
1718const {
@@ -49,7 +50,7 @@ const generateModelScript = (data, logger, cb, app) => {
4950 clearDefinitions ( ) ;
5051 addDefinitions ( convertedExternalDefinitions ) ;
5152 addDefinitions ( convertedModelDefinitions ) ;
52- setUserDefinedTypes ( internalDefinitions ) ;
53+ setUserDefinedTypes ( internalDefinitions , true ) ;
5354 resetDefinitionsUsage ( ) ;
5455
5556 const settings = getSettings ( { containerData, entityData, modelData, references } ) ;
@@ -101,7 +102,7 @@ const generateScript = (data, logger, cb, app) => {
101102
102103 setUserDefinedTypes ( externalDefinitions ) ;
103104 setUserDefinedTypes ( modelDefinitions ) ;
104- setUserDefinedTypes ( internalDefinitions ) ;
105+ setUserDefinedTypes ( internalDefinitions , true ) ;
105106 resetDefinitionsUsage ( ) ;
106107 const isFromUi = options . origin === 'ui' ;
107108
@@ -209,11 +210,19 @@ const convertJsonToAvro = (jsonSchema, schemaName) => {
209210 return resolveUdt ( reorderAvroSchema ( avroSchema ) ) ;
210211} ;
211212
212- const setUserDefinedTypes = definitions => {
213- addDefinitions ( convertSchemaToUserDefinedTypes ( definitions ) ) ;
213+ /**
214+ * When we have a reference in the internal definitions that leads to a definition
215+ * in the model definitions we need to resolve them to avoid creation of a UDT that references
216+ * itself. It may happen when the definition have the same name as the reference.
217+ *
218+ * @param {Array<object> } definitions
219+ * @param {boolean } [resolveReferences]
220+ */
221+ const setUserDefinedTypes = ( definitions , resolveReferences = false ) => {
222+ addDefinitions ( convertSchemaToUserDefinedTypes ( definitions , resolveReferences ) ) ;
214223} ;
215224
216- const convertSchemaToUserDefinedTypes = definitionsSchema => {
225+ const convertSchemaToUserDefinedTypes = ( definitionsSchema , resolveReferences ) => {
217226 definitionsSchema = parseJson ( definitionsSchema ) ;
218227 const definitions = Object . keys ( definitionsSchema . properties || { } ) . map ( key => {
219228 const definition = definitionsSchema . properties [ key ] ;
@@ -230,7 +239,7 @@ const convertSchemaToUserDefinedTypes = definitionsSchema => {
230239 return definitions . reduce (
231240 ( result , { name, schema, customProperties, originalSchema } ) => ( {
232241 ...result ,
233- [ name ] : { schema, customProperties, originalSchema } ,
242+ [ name ] : { schema : resolveReferences ? resolveSchemaUdt ( schema ) : schema , customProperties, originalSchema } ,
234243 } ) ,
235244 { } ,
236245 ) ;
0 commit comments