7

I want to use Mapstruct to map internal models to models generated by OpenApi3 codegen in a Kotlin project.

When I compile the project, it seems that the Mapstruct is not able to find the sources generated by the OpenApi3 codegen plugin, as the resulting implementation contains a NonExistentClass instead of my OpenApi model.

My plugin configuration is

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <configuration>
        <args>
            <arg>-Xjsr305=strict</arg>
        </args>
        <compilerPlugins>
            <plugin>spring</plugin>
            <plugin>jpa</plugin>
        </compilerPlugins>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>

    <executions>
        <execution>
            <id>kapt</id>
            <phase>process-sources</phase>
            <goals>
                <goal>kapt</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/main/kotlin</sourceDir>
                    <sourceDir>src/main/java</sourceDir>
                </sourceDirs>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </execution>

        <execution>
            <id>compile</id>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>test-compile</id>
            <phase>process-test-sources</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>

    </executions>
</plugin>

It seems that the problem is related to kapt finding the generated Java sources.

Is my configuration broken or am I encountering a limitation of the kotlin annotation processor?

EDIT: A simple example to reproduce this can be found here: https://github.com/tobisinghania/kotlin-openapi3-mapstruct-failure

Tobi
  • 2,001
  • 2
  • 27
  • 49
  • When is the OpenApi3 running? During which part of the Maven lifecycle it is getting triggered? – Filip Jan 03 '19 at 09:32
  • In the logs I can see, that the OpenApi plugin is running before the kapt - it seems the sources are there but in the mapstruct execution they are not found. I also tried adding a `generate-sources` tag to the OpenApi plugin, but as expected it did not fix the error. My solution for now is to move the api generation to a different module and have my project depend on that module. This way everything works... – Tobi Jan 03 '19 at 10:38
  • I'm not sure exactly how the kotlin plugin works. Also check the location of the sources for the OpenApi plugin. As from what I see the sources are in your main, which means that it (kapt) would not see them. It should be in generate phase as well, to make sure that it runs before kapt – Filip Jan 03 '19 at 13:21
  • I also figured, that kapt is not able to find the sources and tried adding a source path directly to the `target/generated-sources/...` folder where the sources generated by OpenApi reside but even that did not help... – Tobi Jan 03 '19 at 13:30
  • OK that's weird. If you can provide some reproducal repo, I might be able to look into it – Filip Jan 03 '19 at 14:34
  • Sorry for the late reply, was afk. I created the most simple failing case I could think of: https://github.com/tobisinghania/kotlin-openapi3-mapstruct-failure Thank you for your help! – Tobi Jan 03 '19 at 22:53

1 Answers1

5

The issue is due to the missing <sourceDir>target/generated-sources/openapi/src/main/java</sourceDir> in the kotlin maven plugin configuration

I've made a Pull Request on your repo,

Lautre
  • 155
  • 1
  • 3
  • 12