Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3561;

import org.mapstruct.Condition;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

@Mapper
public interface Issue3561Mapper {

Issue3561Mapper INSTANCE = Mappers.getMapper( Issue3561Mapper.class );

@Mapping(target = "value", conditionQualifiedByName = "shouldMapValue")
Target map(Source source, @Context boolean shouldMapValue);

@Condition
@Named("shouldMapValue")
default boolean shouldMapValue(@Context boolean shouldMapValue) {
return shouldMapValue;
}

class Target {

private final String value;

public Target(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

class Source {

private String value;
private boolean valueInitialized;

public String getValue() {
if ( valueInitialized ) {
return value;
}

throw new IllegalStateException( "value is not initialized" );
}

public void setValue(String value) {
this.valueInitialized = true;
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3561;

import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* @author Filip Hrisafov
*/
@WithClasses(Issue3561Mapper.class)
@IssueKey("3561")
class Issue3561Test {

@ProcessorTest
void shouldCorrectlyUseConditionWithContext() {

Issue3561Mapper.Source source = new Issue3561Mapper.Source();

assertThatThrownBy( () -> Issue3561Mapper.INSTANCE.map( source, true ) )
.isInstanceOf( IllegalStateException.class )
.hasMessage( "value is not initialized" );

Issue3561Mapper.Target target = Issue3561Mapper.INSTANCE.map( source, false );
assertThat( target ).isNotNull();
assertThat( target.getValue() ).isNull();
}
}