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

Should use lombok for generating toString and hashCode #68

Open
xibz opened this issue Jan 5, 2024 · 6 comments
Open

Should use lombok for generating toString and hashCode #68

xibz opened this issue Jan 5, 2024 · 6 comments

Comments

@xibz
Copy link

xibz commented Jan 5, 2024

Right now model classes do not override these methods making, equals, a little more complicated. Lombok can make this easy.

@rjalander
Copy link
Contributor

I am using a maven plugin jsonschema2pojo here to generate the model classes, not sure we can use Lombok with jsonschema2pojo

Today, here is the configuration to generate toStringand hashCode using this plugin - https://github.com/cdevents/sdk-java/blob/main/generator/pom.xml#L92-L93

@xibz
Copy link
Author

xibz commented Jan 17, 2024

If you couldn't do this, Id be very questionable about the library

joelittlejohn/jsonschema2pojo#524 but it looks like you can

@aalmiray
Copy link
Contributor

If you couldn't do this, Id be very questionable about the library

That is not a nice thing to say. Please be respectful.

As @rjalander noted, generated code via schema2pojo does add equals and hashCode but toString is currently disabled. See

<configuration>
<outputDirectory>${sdk.project.dir}/src/main/java</outputDirectory>
<includeHashcodeAndEquals>true</includeHashcodeAndEquals>
<includeToString>false</includeToString>
<addCompileSourceRoot>false</addCompileSourceRoot>
<targetVersion>8</targetVersion>
<includeGeneratedAnnotation>true</includeGeneratedAnnotation>
</configuration>

@rjalander perhaps <includeToString> may be set to true?

Also, @xibz, even though I'm a fan of Lombok it doesn't make sense to add it to this project, nor any other source code generator driven by an annotation processor because this project uses its own source code generator.

@rjalander
Copy link
Contributor

I did not include toString because in these unit tests, I am comparing the expected and actual objects instead of strings for the generated events

@aalmiray
Copy link
Contributor

toString is also useful for logging. Usually it's not a good idea to compare equality using literals, that is why Java has the equals method 😏

@rjalander
Copy link
Contributor

Yes the equals methods is implemented to compare the content of the objects in all the generated models, so it should produce the same result when comparing with objects too.

assertTrue(expectedEvent.getContext().equals(createdEvent.getContext()));
OR
assertEquals(expectedEvent.getContext(), createdEvent.getContext());

But I think we can still implement toString() method too, as it invokes only equals method in either case unless explicitly invoked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants