openapi codegen without access methods - java

I am using the openapi-generator-maven-plugin to generate model sources.
Is there a way to generate them only with fields and without any access methods?
I want the access methods to be generated via lombok with the additionalModelTypeAnnotations configOption in the maven configuration of the openapi-generator-maven-plugin

You can implement those customizations by changing the Mustache templates
Fetch the templates of the Java framework (ie spring-boot) you want to use (ie openapi-generator-cli author template -g spring --additional-properties=library=spring-boot -o tmp/mytemplates
Modify the local model.mustache file to import the packages you want (lombok) and pojo.mustache to remove the getters/setters. There might other customisations necessary (each framework templates are different) but this the recommended approach.

As #beppe suggested in their answer, this can easily be done by modifying the mustache template. However, I highly advice against it. In doing so, you discard many of the the features included with the model objects created by the generator. Some of the features you lose are:
Bean Validation
Jackson Serialization Library Support
Support for vendor extensions
By adding the Lombok annotation to the generated files, you are basically ignoring the main point of the generator, which is to generate files that support each other. You are instead using a generator to call a generator.
Finally, the amount of customization necessary to make the lombok library work with the mustache files is much more than is really worth it. Sure, you can add #Data or #Jacksonized to your model, but what about #JsonIgnore? Do you want to use parameters in your Lombok calls? Because you can't set the builder classname via #Builder(builderClassName = "EmployeeBuilder"). That would be hardcoded. Instead, you'd have to use mustache parameters, such as #Builder(builderClassName = {{$builderClassName}}), which you'd then have to define elsewhere. What about #Builder vs #SuperBuilder? Which one do you use in which situation? How do you define and template it?
At this point, you might as well rewrite the entire pojo.mustache to be able to use the lombok annotations. But, what if you need the old pojo.mustache functionality elsewhere? Now you need to write a custom generator to determine which mustache to use in which situations.
It is best to just build the domain models as the openapi-generator intends and use them as they are. You can add annotations if you need to via vendor extensions such as x-field-extra-annotation and x-class-extra-annotation if you really feel the need to.

Related

Can we use Lombok in Embedded Java?

Can we use lombok framework in embedded Java? Like midlets or implementations where we need only core java.
Yes, I expect that you can use lombok in embedded Java.
Lombok is a compile time dependency, and not related to any deployment platform.
You might want to modify lombok.config and instruct lombok to not generate #ConstructorProperties using lombok.anyConstructor.suppressConstructorProperties=true (in the upcoming release, this will be the default behavior and deprecated), since I expect that that is not available at runtime.
Also, the generated code is optimized for hotspot, so you might also want to configure lombok.equalsAndHashCode.doNotUseGetters=true and lombok.toString.doNotUseGetters=true to use direct field access instead of getters.
Disclosure: I am a lombok developer.

Generate code during automatic build in eclipse?

I am writing a code generator that generates additional classes for each class annotated with a certain annotation. I have other classes in my project that has to refer these generated classes. If I can somehow integrate my code generator into the automatic build process, then I figure the generated code will always stay up to date.
Is this possible? Can I do this if write a new custom builder?
EDIT (2/7):
I looked at Lombok which is doing something similar. However, it modifies the AST from an annotation processor by using undocumented internal methods in eclipse. I don't want to take that risk. Moreover, I think they never wanted to do source generation - just byte code generation.
Assuming you have annotation processing configured, you can use the annotation processing plugin for m2e.
Here's the marketplace link: https://marketplace.eclipse.org/content/m2e-apt

Do Java annotations add notation or functionality?

Are Java annotations used for adding functionality to Java code besides just adding documentation about what's going on in the code? What's the most advanced/complex functionality you could add to your code through an annotation?
Annotation are basically not more than a tag (with optional additional data) on a class/method/field. Other code (libraries or tools) can discover these tags and execute functionality dependant on the annotations found. I don't see a real limit on the complexity of the functionality possibly added by annotations. This can for example emulate AOP (adding functionality before or after a method with an annotation).
Annotations as such only add information (metadata) to a class.
One can easily build a system that uses that metadata to provide additional functionality, however.
For example you can use apt to generate classes based on the information provided by the annotation.
An annotation needs a tool to react to it. If such a tool does not exist the annotation is merely a notation. The "tool" can be an APT based agent or some piece of code that uses reflection (for instance, JUnit's #Test).
Several annotations are recognized by the Java compiler and thus have pre-defined semantics: #Override, #Deprecated, #Target.
I would understand Annotations as a way to document your code in a machine readable way.
For example in Hibernate you can specify the whole persistence information for your objects as annotations. This is directly readable for you and not in a distant xml file. But is also readable for the tool to generate configurations, database schemes etc.

What's the point of package annotations?

I understand the purpose of class annotations, thanks to How and where are Annotations used in Java?. What is the purpose of package annotations, as described in this blog post and ยง7.4.1 of the Java Language Specification?
Why would you want to associate metadata with a package? What kinds of things could you do?
bnd tool (and maven-bundle-plugin) makes use of package annotations. Putting #Version and #Export annotation in package-info.java allows it to generate OSGi metadata.
javadoc uses package annotations.
JAXB uses package-level annotations, for example, to specify mapping of a Java type to XML Schema type package-wide. Package annotations are also used in JBoss's xml binding.
Struts 2 Convention plugin uses an annotation to specify a default interceptor for all actions in a package.
There are some package-level Hibernate Annotations. An example of those annotations' usage can be found here.
I suppose #Deprecated would make sense. And maybe something like #Generated if the whole package was generated by some tool from non-Java source. Or #Internal if this package is not part of a public API.
Maybe OSGi tools (where you need to declare the versions of your packages, and the packages you depend on) could make use of this, too.
Has anyone seen those in the wild?
Two reasons that I can think of:
Annotating special packages to let some aspects (for example using AspectJ) to weave the classes in them for specific functionality.
Annotating some packages that are to be read by some tools, for example for source, meta-data or other kinds of resource generation.
JAXB for example allows most annotations that are normally used on a type to be equally well applied to a package. The meaning in that case would be to specify the default for all classes in that package.
For example, if you want all properties of all classes in a package that are exposed via getter/setters to be mapped in XML you could specify #XmlAccessorType(XMLAccessType.PROPERTY) on each class or simply specify it on the package.
This is not the real purpose, but I'm using them as a workaround to avoid recompilation of the package-info.java files.
The problem is that javac (and the Ant task <javac>) creates no class file for the package-info.java if there is only documentation (the reason for their existence) and the package bla; statement, and that the ant task recompiles every file for which there is no (or an older) corresponding .class file.
Adding a dummy annotation there (like SuppressWarnings) had the effect that a package-info.class is produced and thus the file is not recompiled until changed again.
(Ant 1.8.0 solved this by creating an empty package-info.class, even if there was no annotation, but I'm using an older ant here.)
Test metadata -- that is metadata around test packages (unit tests or otherwise). You can ascribe various pieces of test metadata that are appropriate at the package level, such as: features, owners, versions, bugs/issues, etc. These could be refined at the class or method level, but having package-level definitions or defaults could be handy for brevity. I've utilized a variant of this approach (before the benefit of annotations).
A similar argument could be made for generalized code metadata around the same sorts of things: features, ownership, defects, historical information, etc.

java annotations: library to override annotations with xml files

Java has annotations and that is good. However, some developers feel that it is best to annotate code with metadata using xml files - others prefer annotations but would use metadata to override annotations in source code.
I am writing a Java framework that uses annotations. The question is: is there a standard way to define and parse metadata from xml files. I think this is something every framework that uses annotations could benefit from but I can seem to find something like this on the Internet.
Must I roll my own xml parsing/validation or has someone already done something like this?
There is not a standard way, but here are some Java frameworks who does it:
JPA - check ejb-3_0-fr-spec-persistence.pdf
Spring Framework
TestNG - as written above, though I think it focuses to much on the annotation side rather than the actual configuration he tries to achieve
Seam Framework
I wrote the Annox library which does exactly what you need. With Annox you can read arbitrary annotations from XML.
It's not exactly what you want, but the backport175 project has an implementation of annotations for Java versions before Java 5.
It has some of the functionality you search in that it will read both its own style implementations and "real" annotations if they are present. Maybe this can be used as a starting point to build a more general framework.
Use JAXB http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
You would write the xsd for your metadata file, generate JAXB classes that can help you parse the xml files.

Categories

Resources