flipp-oss / deimos

Framework to work with Kafka, Avro and ActiveRecord
Other
59 stars 22 forks source link

Fix circular reference schema generation #151

Closed iMacTia closed 2 years ago

iMacTia commented 2 years ago

Description

The current implementation of the schema classes generator does not support circular references. Despite these being rare, they can be useful under some circumstances, for example when you want your schema to include a flexible structure.

The fix is relatively straightforward because circular references are already supported by Avro, the issue was that the generator was being caught in an infinite recursive loop (check the first commit on this PR for a reproduction example).

To avoid that, this PR introduces a @discovered_schemas set that memoizes the schemas that have already been processed, and short-circuit the recursive loop on duplicates.

This PR also includes a tiny fix on #child_schemas that filters out primitive schemas when processing a union type (respond_to?(:schemas)). This is not necessarily part of the solution, but I realised it was previously looping through those as well, causing extra no-op processing.

Type of change

How Has This Been Tested?

This fix has been tested according to TDD practices. The first commit contains a failing test case demonstrating the issue. Subsequent commits fix the implementation until the failing test is passing as expected

Checklist:

dorner commented 2 years ago

@iMacTia looks great! The Ruby 3.0 specs are failing though, seems to be something with the number of arguments for a method.

iMacTia commented 2 years ago

That's curious, looking into it now!

iMacTia commented 2 years ago

Well that was quick πŸ˜„! Thanks for nudging me @dorner πŸ™

dorner commented 2 years ago

Nice, thanks for the fix!

iMacTia commented 2 years ago

Love contributing to this gem, feedback/release loop is so quick πŸš€! Thank you @dorner, you're a legend πŸ™‡