Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
#2945 Stabilise top level imports
Make sure that GeneratedType always gets the imported types
from a Type before adding them
  • Loading branch information
filiphr committed Aug 21, 2022
commit 7ba027fc756a99f78b1e6cc4ede8dc45419f86af
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ protected void addIfImportRequired(Collection<Type> collection, Type typeToAdd)
return;
}

if ( needsImportDeclaration( typeToAdd ) ) {
collection.add( typeToAdd );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.. how could this work in the first place? Now we don't add typeToAdd itself, but the required imported types..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at the Type implementation. We are a bit smart there when we need to do an import and only use the TopLevel.Nested for nested. There is a complex logic for computing whether we need to import a type or not.

When I did the initial work for this I changed a lot of places where we were adding types to actually return Type#getImportedTypes, but apparently this is not everywhere (considering the bug reports we've been receiving)

for ( Type type : typeToAdd.getImportTypes() ) {
if ( needsImportDeclaration( type ) ) {
collection.add( type );
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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._2945;

import org.mapstruct.Mapper;
import org.mapstruct.ap.test.bugs._2945._target.Target;
import org.mapstruct.ap.test.bugs._2945.source.Source;
import org.mapstruct.factory.Mappers;

/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue2945Mapper {

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

Target map(Source source);
}
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._2945;

import org.mapstruct.ap.test.bugs._2945._target.EnumHolder;
import org.mapstruct.ap.test.bugs._2945._target.Target;
import org.mapstruct.ap.test.bugs._2945.source.Source;
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;

/**
* @author Filip Hrisafov
*/
@IssueKey("2945")
@WithClasses({
EnumHolder.class,
Issue2945Mapper.class,
Source.class,
Target.class,
})
class Issue2945Test {

@ProcessorTest
void shouldCompile() {
Target target = Issue2945Mapper.INSTANCE.map( new Source( "VALUE_1" ) );

assertThat( target.getProperty() ).isEqualTo( EnumHolder.Property.VALUE_1 );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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._2945._target;

public class EnumHolder {
public enum Property {
VALUE_1,
VALUE_2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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._2945._target;

public class Target {
private EnumHolder.Property property;

public EnumHolder.Property getProperty() {
return property;
}

public void setProperty(EnumHolder.Property property) {
this.property = property;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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._2945.source;

/**
* @author Filip Hrisafov
*/
public class Source {

private final String property;

public Source(String property) {
this.property = property;
}

public String getProperty() {
return property;
}
}