33import java .io .File ;
44import java .nio .file .Files ;
55import java .nio .file .Path ;
6+ import java .nio .file .Paths ;
67import java .util .Arrays ;
78
89import org .eclipse .microprofile .config .Config ;
@@ -41,7 +42,8 @@ private Path getInputBaseDir(final Path sourceDir, final Config config) {
4142 .map (inputBaseDir -> {
4243 int srcIndex = sourceDir .toString ().lastIndexOf ("src" );
4344 return Path .of (sourceDir .toString ().substring (0 , srcIndex ), inputBaseDir );
44- }).orElse (Path .of (sourceDir .toString (), "openapi" ));
45+ })
46+ .orElse (sourceDir .resolve ("openapi" ));
4547 }
4648
4749 @ Override
@@ -54,9 +56,19 @@ public boolean shouldRun(Path sourceDir, Config config) {
5456 return specIsPresent ;
5557 }
5658
59+ @ Override
60+ public Path getInputDirectory () {
61+ return CodeGenProvider .super .getInputDirectory ();
62+ }
63+
5764 @ Override
5865 public boolean trigger (CodeGenContext context ) throws CodeGenException {
59- final Path openApiDir = getInputBaseDir (context .inputDir (), context .config ());
66+
67+ Path projectRoot = findProjectRoot (context .outDir ());
68+
69+ final Path openApiDir = projectRoot != null
70+ ? getInputBaseDir (projectRoot .resolve ("src/main/resources" ), context .config ())
71+ : getInputBaseDir (context .inputDir (), context .config ());
6072
6173 validateOpenApiDir (context , openApiDir );
6274
@@ -122,4 +134,21 @@ private File convertToJSON(Path yamlPath) throws CodeGenException {
122134 throw new CodeGenException ("Error converting YAML to JSON" , e );
123135 }
124136 }
137+
138+ private static Path findProjectRoot (Path outputDirectory ) {
139+ Path currentPath = outputDirectory ;
140+ do {
141+ if (Files .exists (currentPath .resolve (Paths .get ("src" , "main" )))
142+ || Files .exists (currentPath .resolve (Paths .get ("config" , "application.properties" )))
143+ || Files .exists (currentPath .resolve (Paths .get ("config" , "application.yaml" )))
144+ || Files .exists (currentPath .resolve (Paths .get ("config" , "application.yml" )))) {
145+ return currentPath .normalize ();
146+ }
147+ if (currentPath .getParent () != null && Files .exists (currentPath .getParent ())) {
148+ currentPath = currentPath .getParent ();
149+ } else {
150+ return null ;
151+ }
152+ } while (true );
153+ }
125154}
0 commit comments