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 ;
@@ -42,6 +43,7 @@ private Path getInputBaseDir(final Path sourceDir, final Config config) {
4243 int srcIndex = sourceDir .toString ().lastIndexOf ("src" );
4344 return Path .of (sourceDir .toString ().substring (0 , srcIndex ), inputBaseDir );
4445 }).orElse (Path .of (sourceDir .toString (), "openapi" ));
46+
4547 }
4648
4749 @ Override
@@ -56,7 +58,12 @@ public boolean shouldRun(Path sourceDir, Config config) {
5658
5759 @ Override
5860 public boolean trigger (CodeGenContext context ) throws CodeGenException {
59- final Path openApiDir = getInputBaseDir (context .inputDir (), context .config ());
61+
62+ Path projectRoot = findProjectRoot (context .outDir ());
63+
64+ final Path openApiDir = projectRoot != null
65+ ? getInputBaseDir (projectRoot .resolve ("src/main/resources" ), context .config ())
66+ : getInputBaseDir (context .inputDir (), context .config ());
6067
6168 validateOpenApiDir (context , openApiDir );
6269
@@ -122,4 +129,21 @@ private File convertToJSON(Path yamlPath) throws CodeGenException {
122129 throw new CodeGenException ("Error converting YAML to JSON" , e );
123130 }
124131 }
132+
133+ private static Path findProjectRoot (Path outputDirectory ) {
134+ Path currentPath = outputDirectory ;
135+ do {
136+ if (Files .exists (currentPath .resolve (Paths .get ("src" , "main" )))
137+ || Files .exists (currentPath .resolve (Paths .get ("config" , "application.properties" )))
138+ || Files .exists (currentPath .resolve (Paths .get ("config" , "application.yaml" )))
139+ || Files .exists (currentPath .resolve (Paths .get ("config" , "application.yml" )))) {
140+ return currentPath .normalize ();
141+ }
142+ if (currentPath .getParent () != null && Files .exists (currentPath .getParent ())) {
143+ currentPath = currentPath .getParent ();
144+ } else {
145+ return null ;
146+ }
147+ } while (true );
148+ }
125149}
0 commit comments