Openapi generator: Add import class to generated model - java

I am using Openapi generator(5.4.0), with spring (generator name) and gradle, I am trying to add an import to a generated model.
For the particular field in the api spec, I have added:
x-field-extra-annotation: "#com.fasterxml.jackson.annotation.JsonFormat ...."
This works, however I dont want to fully qualify it, and have the com.fasterxml.jackson.annotation.JsonFormat import added.
I tried adding typeMappings to genratedCode task, but that doesn't work.
importMappings = [
'JsonFormat' : 'com.fasterxml.jackson.annotation.JsonFormat'
]
Update:
I can add model.mustache template to project, and add the import. ie
{{#useBeanValidation}}
...
import com.fasterxml.jackson.annotation.JsonFormat;
...
{{/useBeanValidation}}
Any ideas? Better ways.
Thanks.

There is no production-ready solution (yet!)
If you are not starving for this feature, wait for acceptance of related issue and PR to one of the nearest releases.
https://github.com/OpenAPITools/openapi-generator/issues/13938
It will allow required customizations using
x-spring-constraint: #MyAnnotation(value = 1, message = "nice") (or x-java-constraint) extension with configOption
customValidationAnnotationsPackages="my.custom.package.MyAnnotaion;my.other.package.*"
Else, refer to https://bartko-mat.medium.com/openapi-generator-to-spring-boot-with-custom-java-validations-623381df9215 as tutorial for handmade solution

Related

Annotation type not applicable to this kind of declaration error in package info class in Java 8

I have a package info class, it used to compile in Java 6/7. But in Java 8, I get compilation errors:
*****error: annotation type not applicable to this kind of declaration
#NamedNativeQueries({
error: annotation type not applicable to this kind of declaration
#SqlResultSetMappings({*****
This is the code:
#NamedNativeQueries({
#NamedNativeQuery(name = "",
query = "",
resultSetMapping = "mapping"
),
NamedNativeQuery(
name = "",
query = "",
callable = true,
readOnly = false,
resultSetMapping = ""
)
})
#SqlResultSetMappings({
})
package abc.domain;
import javax.persistence.ColumnResult;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;
Thanks in advance for your help
EDIT: Additional comments have made more clear what's going wrong here; as a consequence, the nature of this answer has changed somewhat.
It sounds like you switched from Hibernate's own version of #NamedNativeQueries and company, which you can stick on packages, to the general javax persistence variant, which cannot be placed on packages.
You must have removed and re-generated the imports in your attempt to convert this code. Don't do that - remove all those imports and replace them with import org.hibernate.annotations.NamedNativeQueries and friends instead.
For posterity, the original answer, still valid but only in context specifically for javax.persistence.NamedNativeQueries.
You must be misremembering; it does not and never has worked on java7 (that's the java7 docs of amedNativeQueries - note how it has a #Target(value=TYPE) marker, so it cannot be put on a package, and that's the v7 edition of the docs!)
You put such things on a type, for example the top level type. Which means the annotations appear near the top, but after the package statement. Given that you're using them in a package-info.java file, they simply cannot appear here (and never could).
SqlResultSetMappings is the same.
There's a small chance that somehow javac7 didn't actually check the Target condition of these annotations. However, that simply means that your code never worked, even if it did compile.

Lombok now applies #NonNull checks also for com.mongodb.lang.NonNull (and possibly others)

Let's have a simple MongoDB Document class with Lombok annotations
import lombok.Data;
import com.mongodb.lang.NonNull;
import org.springframework.data.mongodb.core.mapping.Document;
#Document
#Data
public class Car {
#NonNull
private String plate;
}
and simple test
#Test
public void test() {
Car c = new Car();
c.setPlate(null);
}
With Lombok v1.18.22 this worked well and test succeeded. However since v1.18.24, the test starts to fail since apparently Lombok now understands more annotations that indicate that field/param shouldn't be null
https://projectlombok.org/changelog
I'm not sure if MongoDB uses these #NonNull annotations for anything (I think not?) but at least for a documentation purposes it's nice to have them. Also the field can be null in some cases for MongoDB documents f.e. when using Spring Data's filtering using Example
repository.findByExample(Example.of(car))
where I might filter by only some specific field, leaving also mandatory fields to null.
If it'd be about #lombok.NonNull then I'd simply remove it but I don't want to remove Mongo annotations just because of Lombok. Thus my question:
Question 1: Can I avoid these checks? Or can I turn this feature off? So either that Lombok will consider only lombok.* annotations or just suppress generation of null checks in general.
Second issue is that Intellij Idea Lombok plugin apparently doesn't know about this feature yet so it considers that code to be valid. The build then fails as Lombok v1.18.24 generates constructor with all the #NonNull fields so it can't call new Car(). With #lombok.NonNull it works well in Intellij but with Mongo's NonNull it doesn't. I'm using IntelliJ IDEA 2022.1.3 (Community Edition)
Question 2: Is this a bug in Intellij Idea or its Lombok plugin? If yes is it known? I haven't found any info about it which leads me to the idea that the error is on my side

What to replace the DatasourceConnectionProviderImpl class?

In the project I work with Hibernate ORM version 4.2.6.Final previously was used. Now I'm trying to update it to the latest release, which is 4.3.10.Final. However, org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl is no longer mentioned in the docs and no longer supplied.
This is how this class is used in the code I work with:
( (DatasourceConnectionProviderImpl) (
(SessionFactoryImpl) getDAO().getSessionFactory() )
.getConnectionProvider() )
.setDataSource(ds);
What can it be replaced with? And where can I find the mention of its removal in the Hibernate docs or release notes?
Finally, by using GrepCode.com multi-repository search engine I figured out that DatasourceConnectionProviderImpl class was not actually removed. Instead, since Hibernate 4.3.0, some classes are moved:
from org.hibernate.service.jdbc.connections.internal
to org.hibernate.engine.jdbc.connections.internal.
So import statement has to be changed to
import org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl;

Unable to create parboiled parser

I have created a parser class for the parboiled framework according to this simple example:
package my.package;
import org.parboiled.BaseParser;
import org.parboiled.annotations.BuildParseTree;
#BuildParseTree
public class QueryParser extends BaseParser<Object> {
//some rules
}
If I try to create parser as shown in the example
QueryParser parser = Parboiled.createParser(QueryParser.class);
I get an exception at that line:
java.lang.ClassCastException: my.package.QueryParser$$parboiled cannot be cast to org.parboiled.BaseParser
at org.parboiled.Parboiled.createParser(Parboiled.java:56)
...
I'm really not doing anything special that is not done in the example. The only difference is that the parser and and the class calling it are in different projects but I can't imagine why this should matter. The dependencies between the projects (which are Eclipse plugin projects) should be alright.
Can anyone tell what I'm doing wrong or where the mistake could be?
It actually seems to have something to do with the run configurations. I moved all the parboiled relevant code to one project and it works. I think I'll keep it this way because it is better encapsulation anyway.

Accessing the application.conf properties from java class with Play! 2.0

I want to add an object to the Global scope, and in order to construct it I need to pass it a path to a file.
I don't want to hard code the file path in the source, and so I want to get that path from the application.conf.
The problem is that I don't know how to access these properties from the java class.
I tried this:
Configuration.root().getString("file.path")
But it ends with a NullPointerException.
Am I wrong in assuming that there's a global Configuration instance that I can use?
Thanks.
Try Play.application().configuration().getString("your.key")
As noted in the comment (nico_ekito), please use play.Play and not play.api.Play. play.api.Play is for scala controllers (see comment by Marcus biesior Biesioroff)
Additionally, play uses https://github.com/typesafehub/config under the hood so it can also provide some insights.
Even if it seems simple, here is the scala way to get properties from configuration file :
Play 2.0 and 2.1 :
import play.api.Play.current
...
Play.application.configuration.getString("your.key")
Play 2.2 and +
import play.api.Play.current
...
current.configuration.getString("your.key")
Using Typesafe config
import com.typesafe.config.ConfigFactory
...
ConfigFactory.load().getString("your.key");
From Play 2.4 and + it is better to use dependency injection to access Configurations:
import play.Configuration;
import javax.inject.Inject;
#Inject
private Configuration configuration;
...
String value = configuration.getString("your.key");
Since Play 2 uses the Typesafe config library, I accessed my vars in application.conf like this :
ConfigFactory.load().getString("my.var");
In the play java is:
import play.Play;
...
Play.application().configuration().getString("key")
Use as following (Tested in Play 1.2.5)
${play.configuration.getProperty('my.var')}
where my.var should be specified in application.conf file
As a reference to access it from the template (for play < 2)
play.configuration['your.key']
As folks have mentioned, Play.application.configuration no longer exists.
In Play Scala 2.3.x, to read a value from conf/application.conf, you can do the following:
import play.api.Play.current
...
current.configuration.getString("key")
In Play 1.2.x
import play.Play;
...
String version = Play.configuration.getProperty("application.version.number", "1.1.1");
where the second parameter is the default value
Import this
import com.typesafe.config.Config;
and write the below lines
private Config config;
this.config = ConfigProvider.config();
String value = this.config.getString("fieldFromConfigFile");
import play.Play;
String myVal = Play.configuration.getProperty("your.key").toString();
i use this in my app and it works
Dont forget to import play.Play. Hope it'll gives you help
Starting from version 2.5 please use play.Application class which should be injected and then
application.config().getString("your.property.here")
For Java Playframework:
In Application.conf, you can put something like that:
email="example#gmail.com.pe"
some class:
import play.Play;
String email = Play.application().configuration().getString("key") // key ->email

Categories

Resources