Problem with NullValueCheckStrategy.ALWAYS (mapstruct 1.3.1) - java

After upgrading mapstruct from 1.2.0 to 1.3.1 I noticed that the annotation #Mapper(nullValueCheckStrategy=NullValueCheckStrategy.ALWAYS) in not in effect.
Is it a bug of new mapstruct version?
Example:
The code below:
String id = getTestId( testId);
if ( id != null ) {
testCase.setTestCaseId( id );
}
else {
testCase.setTestCaseId( null );
}
while the right is:
String id = getTestId( testId);
if ( id != null ) {
testCase.setTestCaseId( id );
}

The behaviour has been made more consequent with the advent of NullValuePropertyMapping. I think that was mentioned in the release notes as well. Checkout the documentation:
1: update methods (#MappingTarget)
https://mapstruct.org/documentation/stable/reference/html/#mapping-result-for-null-properties
2: regular (non update) methods
https://mapstruct.org/documentation/stable/reference/html/#checking-source-property-for-null-arguments

Related

How to generate pojos with default constructor and setters

Firstly, is this is a good idea (to generate/use pojo with default constructor and setters) ?
If yes, how is it possible by using the code generator ?
I'm using the nu.studer.jooq Gradle plugin (https://github.com/etiennestuder/gradle-jooq-plugin) with jooq 3.11.9 and a postgres db.
// Generator
jooq {
version = '3.11.9'
edition = 'OSS'
sample(sourceSets.main) {
jdbc {
driver = 'org.postgresql.Driver'
url = 'jdbc:postgresql://localhost:5432/mydb'
user = 'myuser'
password = 'mypwd'
// properties {
// property {
// key = 'ssl'
// value = 'true'
// }
// }
}
generator {
name = 'org.jooq.codegen.DefaultGenerator'
strategy {
name = 'org.jooq.codegen.DefaultGeneratorStrategy'
}
database {
name = 'org.jooq.meta.postgres.PostgresDatabase'
inputSchema = 'public'
forcedTypes {
forcedType {
name = 'varchar'
expression = '.*'
types = 'JSONB?'
}
forcedType {
name = 'varchar'
expression = '.*'
types = 'INET'
}
}
}
generate {
relations = true
deprecated = false
records = true
immutablePojos = true
fluentSetters = true
}
target {
packageName = 'com.project.generated'
directory = 'src/main/java'
}
}
}
}
Thank in advance for your ideas and advices.
Firstly, is this is a good idea (to generate/use pojo with default constructor and setters) ?
A good idea for doing what? The jOOQ code generator doesn't judge you. Using default constructors and getters/setters is one supported flavour. Immutable POJOs is the other one.
If yes, how is it possible by using the code generator ?
Turn off the genreation of immutablePojos
pojos = true
immutablePojos = false
Note that you don't have to turn that off. The default is for them to be turned off.
I just released a code generator which does exactly what you want. (If i got you right)
Try https://github.com/carstenSpraener/cgV19 ist has a PoJo generator in it's HelloWorld exammple. Supports
gradle plugin
modular model loader
"cartridges" for different needs:
cartridgeA: PoJo in Java
cartridgeB: PoJo in TypeScript

Java Spring Swagger Startuptime Startupbehaviour

I am having a Springboot application where I integrated Swagger for creating REST documentations.
When using swagger my startup time increases dramatically by factor 5. I'm having 30secs without swagger. 2min plus with swagger.
Also having the docket listed in the snipped underneath startup time becomes endlessly! - And this is a no-go.
My Problem is endless startup times + Exceptions regarding memory usage from google guava.
Why is it that way? What am I doing wrong? Where is the missing point? Why is the API not protecting me from very wrong setups?
It feels like that swagger is on the one hand a number one tool for documenting rest APIs but using it feels very ancient for me.
Some setup info (located at maven pom.xml):
- org.springframework.boot; spring-boot-starter-parent; 1.5.5.RELEASE
- io.springfox; springfox-swagger2; 2.9.2
- io.springfox; springfox-swagger-ui; 2.9.2
I somewhere picked up that googles guava library needs to be exchanged; which I did: https://github.com/springfox/springfox/issues/2616#issuecomment-412811912
Is swagger/springfox really so good to use for documenting an API coming from Spring? - What would be some alternatives to render documentation?
#Configuration
#EnableSwagger2
public class Swagger2UiConfiguration extends WebMvcConfigurerAdapter {
...
#Bean(name="restPublicSwaggerV1Api")
public Docket publicV1Api(BuildProperties build) {
final ApiInfoBuilder apiInfo = new ApiInfoBuilder()
.title(build.getName())
.description("Description for public API")
.version( build.getVersion() );
final long TIME = System.currentTimeMillis();
final Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName( "public-v1" )
.apiInfo( apiInfo.build() )
.select()
.apis( (wmrh)->{ // springfox.documentation.spring.web.WebMvcRequestHandler
final StringBuffer sb = new StringBuffer();
sb.append( wmrh.getClass().getTypeName() +"\n\t"+ wmrh );
final RequestHandlerKey rhk = wmrh.key();
boolean result = false;
for( String pathMapping : rhk.getPathMappings() ) {
sb.append( "\n\t-> "+ pathMapping );
result |= pathMapping.startsWith( "/api/public/" );
}
sb.append( "\n\t=>> "+ result +", time: "+ Util.formatTime( System.currentTimeMillis() - TIME ) +" after start." );
LOG.debug( sb.toString() );
return result;
} )
.paths( (p)->{ return true; } )
.build();
LOG.debug( "instantiated: {}", docket );
return docket;
}
Try using latest swagger-ui(swagger-js 2.1.17) as specified in
https://github.com/swagger-api/swagger-ui/issues/1919
Use #JsonIgnore in Many-To-Many relations

Why does my JGit checkout return null ref?

I've the following code:
fun checkoutBranch(path: Path, name: String) {
Git.open(path.toFile()).use { git ->
val branchExists = git
.branchList()
.setListMode(ListBranchCommand.ListMode.ALL)
.call()
.filterNot { it.name.startsWith("refs/remotes/") }
.map { it.name }
.any { it.endsWith(name) }
val ref = git
.checkout()
.setCreateBranch(!branchExists)
.setName(name)
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
.call()
}
}
When I call it with name = master, everything works as expected. A subsequent call with name = test causes a new branch to be created, but ref is null. Looking at CheckoutCommand#L285, it seems that ref.name = refs/heads/master for master, but for test, ref.name = refs/tags/test, and ref is then set to null.
Ref ref = repo.findRef(name);
if (ref != null && !ref.getName().startsWith(Constants.R_HEADS))
ref = null;
What is happening here? Is this the expected behavior for a new branch? By going into the repo, I can see that it is in detached HEAD state, perhaps causing this issue.
Thanks to #ElpieKay for the clue. The problem was caused by the presence of a tag named test. Apparently, JGit prefers tags over branches when looking for references.
I solved the issue by explicitly specifying the branch name as refs/heads/test (in setName).

avoid null pointer excpetion while parsing the xml

This is my xml to parse
<metadata>
<groupId>org.chromium</groupId>
<artifactId>chromedriver-win32</artifactId>
<versioning>
<release>2.14</release>
<versions>
<version>2.14</version>
</versions>
<lastUpdated>20150610112540</lastUpdated>
</versioning>
</metadata>
While I am trying to parse the above XML, I am getting NullPointerException even though I have handled the null check. Below is the code
private String getVersionFromNode(Element eElement) {
String version = null;
// Get the latest version
String tagName = "latest";
try {
version = eElement.getElementsByTagName(tagName).item(0).getTextContent();
// If latest version is not available take the release
// version
if ( version.isEmpty() || null == version || "null" == version) {
tagName = "release";
version = eElement.getElementsByTagName(tagName).item(0).getTextContent();
}
}
catch (NullPointerException e) {
System.out.println("not able to parse " + tagName + " tag " + e.getMessage());
}
return version;
}
When the version is null, it should enter in below block.
if ( version.isEmpty() || null == version || "null" == version) {
tagName = "release";
version = eElement.getElementsByTagName(tagName).item(0).getTextContent();
}
What should be solution for this problem?
Your String tagName is "latest" and in your xml file, there is no element named latest and so your version remains null and therefore a NullPointerException is generated when the code:
version.isEmpty()
is executed in your if statement. Because version is null. And your code tries to check if null is empty. Which it cannot. And so, a NullPointerException is generated here.
To handle this situation, first check if version is null. Secondly check if the string version is equal to string "null". And thirdly check if it is empty. So change the sequence of conditions in your if statement from:
if ( version.isEmpty() || null == version || "null" == version)
to
if (null == version || "null" == version || version.isEmpty())
By the way, as a side tip, use:
"null".equals(version)
rather than
"null" == version
Change your condition
if ( version.isEmpty() || null == version || "null" == version)
to
if (null == version || "null".equals(version) || version.isEmpty())
When chaining together method calls, you risk a NullPointerException at every step. For example, in your line
version = eElement.getElementsByTagName(tagName).item(0).getTextContent();
there are 4 opportunities for a NullPointerException:
if eElement is null, trying to call getElementsByTagName(tagName)
if calling getElementsByTagName(tagName) when tagName is null
if getElementsByTagName(tagName) returns null, trying to call item(0)
if item(0) returns null, trying to call getTextContent()
Not all of these will be possible in your code because tagName is specified, but you should always beware.
An alternative approach for your problem would be to use XPath
See also How to read XML using XPath in Java

java hibernate criteria issue

I did the following code I have the error duplicate field : owner.
I tried to create an alias, but it's the same for country, ...
And when i create an alias for each I have this error :
could not resolve property: country of: com.something.test.bo.impl.Link
Code:
if (link.getOwner() != null) {
if (link.getOwner().getSiteName().trim().length() > 0 ) {
criteria.createCriteria("owner").add(Restrictions.like("siteName", link.getOwner().getSiteName()));
}
if (link.getOwner().getCountry().getName().trim().length() > 0 ) {
criteria.createCriteria("owner").createCriteria("country").add(Restrictions.like("name", link.getOwner().getCountry().getName()));
}
if (link.getOwner().getCountry().getZone().getName().trim().length() > 0 ) {
criteria.createCriteria("owner").createCriteria("country").createCriteria("zone").add(Restrictions.like("name", link.getOwner().getCountry().getZone().getName()));
}
if (link.getOwner().getCountry().getZone().getRegion().getCode().trim().length() > 0 ) {
criteria.createCriteria("owner").createCriteria("country").createCriteria("zone").createCriteria("region").add(Restrictions.like("code", link.getOwner().getCountry().getZone().getRegion().getCode()));
}
}
Does anybody have an idea to make this search ?
I think you should be using "if else". As I don't find any ambiguity in your criteria creation.

Categories

Resources