Skip to content

Commit

Permalink
Add overloads for local files in OpenAPI extension (#899)
Browse files Browse the repository at this point in the history
Fixes #887
  • Loading branch information
melix authored Dec 4, 2023
1 parent fd6f6eb commit 7ef9c39
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ public void client(String name, Provider<RegularFile> definition, Action<? super
}
}

@Override
public void server(String name, File definition, Action<? super OpenApiServerSpec> spec) {
server(name, project.getObjects().fileProperty().fileValue(definition), spec);
}

@Override
public void client(String name, File definition, Action<? super OpenApiClientSpec> spec) {
client(name, project.getObjects().fileProperty().fileValue(definition), spec);
}

private static Provider<Directory> mainSrcDir(AbstractOpenApiGenerator<?, ?> t, String language) {
return t.getOutputDirectory().dir("src/main/" + language);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ public interface OpenApiExtension {
* @param spec configuration for the server generation
*/
void client(String name, Provider<RegularFile> definition, Action<? super OpenApiClientSpec> 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<? super OpenApiServerSpec> 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<? super OpenApiClientSpec> spec);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 7ef9c39

Please sign in to comment.