diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/DefaultOpenApiExtension.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/DefaultOpenApiExtension.java index 03afbe04..225492e2 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/DefaultOpenApiExtension.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/DefaultOpenApiExtension.java @@ -223,6 +223,16 @@ public void client(String name, Provider definition, Action spec) { + server(name, project.getObjects().fileProperty().fileValue(definition), spec); + } + + @Override + public void client(String name, File definition, Action spec) { + client(name, project.getObjects().fileProperty().fileValue(definition), spec); + } + private static Provider mainSrcDir(AbstractOpenApiGenerator t, String language) { return t.getOutputDirectory().dir("src/main/" + language); } diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiExtension.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiExtension.java index 88081d39..c6c3289c 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiExtension.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiExtension.java @@ -62,4 +62,20 @@ public interface OpenApiExtension { * @param spec configuration for the server generation */ void client(String name, Provider definition, Action spec); + + /** + * Configures generation of a server, given a definition file. + * @param name an identifier used to uniquely refer to the generator, used to derive task names + * @param definition the OpenAPI definition file provider + * @param spec configuration for the server generation + */ + void server(String name, File definition, Action spec); + + /** + * Configures generation of a server, given a definition file. + * @param name an identifier used to uniquely refer to the generator, used to derive task names + * @param definition the OpenAPI definition file provider + * @param spec configuration for the server generation + */ + void client(String name, File definition, Action spec); } diff --git a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiClientGeneratorSpec.groovy b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiClientGeneratorSpec.groovy index 23cef57b..8d375d62 100644 --- a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiClientGeneratorSpec.groovy +++ b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiClientGeneratorSpec.groovy @@ -98,4 +98,55 @@ class OpenApiClientGeneratorSpec extends AbstractOpenApiGeneratorSpec { and: file("build/generated/openapi/generateClientOpenApiModels/src/main/java/io/micronaut/openapi/model/Pet.java").exists() } + + def "can generate an java OpenAPI client implementation using local file and custom name"() { + given: + settingsFile << "rootProject.name = 'openapi-client'" + buildFile << """ + plugins { + id "io.micronaut.minimal.application" + id "io.micronaut.openapi" + } + + micronaut { + version "$micronautVersion" + openapi { + client("myClient", file("petstore.json")) { + lang = "java" + lombok = true + useReactive = true + generatedAnnotation = false + fluxForArrays = true + } + } + } + + $repositoriesBlock + + dependencies { + + annotationProcessor "org.projectlombok:lombok" + annotationProcessor "io.micronaut.serde:micronaut-serde-processor" + + compileOnly "org.projectlombok:lombok" + + implementation "io.micronaut.serde:micronaut-serde-jackson" + implementation "io.micronaut.reactor:micronaut-reactor" + } + + """ + + withPetstore() + + when: + def result = build('test') + + then: + result.task(":generateMyClientOpenApiApis").outcome == TaskOutcome.SUCCESS + result.task(":generateMyClientOpenApiModels").outcome == TaskOutcome.SUCCESS + result.task(":compileJava").outcome == TaskOutcome.SUCCESS + + and: + file("build/generated/openapi/generateMyClientOpenApiModels/src/main/java/io/micronaut/openapi/model/Pet.java").exists() + } } diff --git a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiServerGeneratorSpec.groovy b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiServerGeneratorSpec.groovy index ca53b472..71059ecf 100644 --- a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiServerGeneratorSpec.groovy +++ b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiServerGeneratorSpec.groovy @@ -106,4 +106,61 @@ class OpenApiServerGeneratorSpec extends AbstractOpenApiGeneratorSpec { file("build/classes/java/main/io/micronaut/openapi/api/PetApi.class").exists() file("build/classes/java/main/io/micronaut/openapi/model/Pet.class").exists() } + + def "can generate an java OpenAPI server implementation with custom name and local file"() { + given: + settingsFile << "rootProject.name = 'openapi-server'" + buildFile << """ + plugins { + id "io.micronaut.minimal.application" + id "io.micronaut.openapi" + } + + micronaut { + version "$micronautVersion" + runtime "netty" + testRuntime "junit5" + openapi { + server("myServer", file("petstore.json")) { + lang = "java" + lombok = true + useReactive = true + generatedAnnotation = false + fluxForArrays = true + aot = true + } + } + } + + $repositoriesBlock + mainClassName="example.Application" + + dependencies { + + annotationProcessor "org.projectlombok:lombok" + annotationProcessor "io.micronaut.serde:micronaut-serde-processor" + + compileOnly "org.projectlombok:lombok" + + implementation "io.micronaut.security:micronaut-security" + implementation "io.micronaut.serde:micronaut-serde-jackson" + } + """ + + withPetstore() + + when: + def result = build('test') + + then: + result.task(":generateMyServerOpenApiApis").outcome == TaskOutcome.SUCCESS + result.task(":generateMyServerOpenApiModels").outcome == TaskOutcome.SUCCESS + result.task(":compileJava").outcome == TaskOutcome.SUCCESS + + and: + file("build/generated/openapi/generateMyServerOpenApiApis/src/main/java/io/micronaut/openapi/api/PetApi.java").exists() + file("build/generated/openapi/generateMyServerOpenApiModels/src/main/java/io/micronaut/openapi/model/Pet.java").exists() + file("build/classes/java/main/io/micronaut/openapi/api/PetApi.class").exists() + file("build/classes/java/main/io/micronaut/openapi/model/Pet.class").exists() + } }