Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mapping entry point generator properties to client properties #1459

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
def openapiGenerate = tasks.register("generateOpenApi", OpenApiGeneratorTask) {
lang = "java"
generatedAnnotation = true
clientId = "myClient"
ksp = false
classpath.from(configurations.openapiGenerator)
openApiDefinition.convention(layout.projectDirectory.file("petstore.json"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
def openapiGenerate = tasks.register("generateOpenApi", OpenApiGeneratorTask) {
lang = "kotlin"
generatedAnnotation = true
clientId = "myClient"
ksp = false
classpath.from(configurations.openapiGenerator)
openApiDefinition.convention(layout.projectDirectory.file("petstore.json"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
def openapiGenerate = tasks.register("generateOpenApi", OpenApiGeneratorTask) {
lang = "kotlin"
generatedAnnotation = true
clientId = "myClient"
ksp = true
classpath.from(configurations.openapiGenerator)
openApiDefinition.convention(layout.projectDirectory.file("petstore.json"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public abstract class OpenApiGeneratorTask extends DefaultTask {
@Input
public abstract Property<Boolean> getKsp();

@Input
public abstract Property<String> getClientId();

@OutputDirectory
public abstract DirectoryProperty getOutputDirectory();

Expand Down Expand Up @@ -120,9 +123,6 @@ public void execute() throws IOException {
var generatedTestSourcesDir = getGeneratedTestSourcesDirectory().get().getAsFile();
var lang = getLang().get();
var generatedAnnotation = getGeneratedAnnotation().get();
var ksp = getKsp().get();

var apiPrefix =

Files.createDirectories(generatedSourcesDir.toPath());
Files.createDirectories(generatedTestSourcesDir.toPath());
Expand All @@ -139,12 +139,13 @@ public void execute() throws IOException {
args.add(getResponseBodyMappings().get().toString());
args.add(lang.toUpperCase());
args.add(Boolean.toString(generatedAnnotation));
args.add(Boolean.toString(ksp));
args.add(Boolean.toString(getKsp().get()));
args.add(getNameMapping().get().toString());
args.add(getApiNamePrefix().isPresent() ? getApiNamePrefix().get() : "");
args.add(getApiNameSuffix().isPresent() ? getApiNameSuffix().get() : "");
args.add(getModelNamePrefix().isPresent() ? getModelNamePrefix().get() : "");
args.add(getModelNameSuffix().isPresent() ? getModelNameSuffix().get() : "");
args.add(getClientId().getOrElse(""));
args.add(getApiNamePrefix().getOrElse(""));
args.add(getApiNameSuffix().getOrElse(""));
args.add(getModelNamePrefix().getOrElse(""));
args.add(getModelNameSuffix().getOrElse(""));
javaexec.args(args);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,14 @@ public void configureJavaClientOptions() {
if (javaClientOptions.additionalClientTypeAnnotations() != null) {
javaClientCodegen.setAdditionalClientTypeAnnotations(javaClientOptions.additionalClientTypeAnnotations());
}
if (javaClientOptions.clientId() != null) {
javaClientCodegen.setClientId(javaClientCodegen.clientId);
if (javaClientOptions.clientId() != null && !javaClientOptions.clientId().isBlank()) {
javaClientCodegen.setClientId(javaClientOptions.clientId());
}
if (javaClientOptions.authorizationFilterPattern() != null) {
javaClientCodegen.setAuthorizationFilterPattern(javaClientCodegen.authorizationFilterPattern);
javaClientCodegen.setAuthorizationFilterPattern(javaClientOptions.authorizationFilterPattern());
}
if (javaClientOptions.basePathSeparator() != null) {
javaClientCodegen.setBasePathSeparator(javaClientCodegen.basePathSeparator);
javaClientCodegen.setBasePathSeparator(javaClientOptions.basePathSeparator());
}
javaClientCodegen.setConfigureAuthorization(javaClientOptions.useAuth());
javaClientCodegen.setLombok(javaClientOptions.lombok());
Expand Down Expand Up @@ -318,14 +318,14 @@ public void configureKotlinClientOptions() {
if (kotlinClientOptions.additionalClientTypeAnnotations() != null) {
kotlinClientCodegen.setAdditionalClientTypeAnnotations(kotlinClientOptions.additionalClientTypeAnnotations());
}
if (kotlinClientOptions.clientId() != null) {
kotlinClientCodegen.setClientId(kotlinClientCodegen.clientId);
if (kotlinClientOptions.clientId() != null && !kotlinClientOptions.clientId().isBlank()) {
kotlinClientCodegen.setClientId(kotlinClientOptions.clientId());
}
if (kotlinClientOptions.authorizationFilterPattern() != null) {
kotlinClientCodegen.setAuthorizationFilterPattern(kotlinClientCodegen.authorizationFilterPattern);
kotlinClientCodegen.setAuthorizationFilterPattern(kotlinClientOptions.authorizationFilterPattern());
}
if (kotlinClientOptions.basePathSeparator() != null) {
kotlinClientCodegen.setBasePathSeparator(kotlinClientCodegen.basePathSeparator);
kotlinClientCodegen.setBasePathSeparator(kotlinClientOptions.basePathSeparator());
}
kotlinClientCodegen.setGeneratedAnnotation(kotlinClientOptions.generatedAnnotation());
kotlinClientCodegen.setKsp(kotlinClientOptions.ksp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,35 @@ public class GeneratorMain {
*/
public static void main(String[] args) throws URISyntaxException {
boolean server = "server".equals(args[0]);
var lang = GeneratorLanguage.valueOf(args[6].toUpperCase());
var generatedAnnotation = Boolean.parseBoolean(args[7]);
var ksp = Boolean.parseBoolean(args[8]);
var definitionFile = new URI(args[1]);
var outputDirectory = new File(args[2]);

var outputKinds = Arrays.stream(args[3].split(","))
.map(MicronautCodeGeneratorEntryPoint.OutputKind::of)
.toArray(MicronautCodeGeneratorEntryPoint.OutputKind[]::new);

List<ParameterMapping> parameterMappings =
parseParameterMappings(args[4]);
List<ResponseBodyMapping> responseBodyMappings =
parseResponseBodyMappings(args[5]);
var lang = GeneratorLanguage.valueOf(args[6].toUpperCase());
var generatedAnnotation = Boolean.parseBoolean(args[7]);
var ksp = Boolean.parseBoolean(args[8]);

Map<String, String> nameMapping = parseNameMapping(args[9]);

String apiPrefix = args[10];
String apiSuffix = args[11];
String modelPrefix = args[12];
String modelSuffix = args[13];
String clientId = args[10];

MicronautCodeGeneratorEntryPoint.OutputKind[] outputKinds
= Arrays.stream(args[3].split(","))
.map(MicronautCodeGeneratorEntryPoint.OutputKind::of)
.toArray(MicronautCodeGeneratorEntryPoint.OutputKind[]::new);
String apiPrefix = args[11];
String apiSuffix = args[12];
String modelPrefix = args[13];
String modelSuffix = args[14];

var builder = MicronautCodeGeneratorEntryPoint.builder()
.withDefinitionFile(new URI(args[1]))
.withOutputDirectory(new File(args[2]))
.withDefinitionFile(definitionFile)
.withOutputDirectory(outputDirectory)
.withOutputs(outputKinds)
.withOptions(options -> {
.withOptions(options ->
options.withLang(lang)
.withInvokerPackage("io.micronaut.openapi.test")
.withApiPackage("io.micronaut.openapi.test.api")
Expand All @@ -91,39 +95,45 @@ public static void main(String[] args) throws URISyntaxException {
.withTestFramework(lang == JAVA ? MicronautCodeGeneratorEntryPoint.TestFramework.SPOCK : MicronautCodeGeneratorEntryPoint.TestFramework.JUNIT5)
.withParameterMappings(parameterMappings)
.withResponseBodyMappings(responseBodyMappings)
.withNameMapping(nameMapping);
});
.withNameMapping(nameMapping)
);
if (server) {
if (lang == GeneratorLanguage.KOTLIN) {
builder.forKotlinServer(serverOptions -> {
serverOptions.withControllerPackage("io.micronaut.openapi.test.controller");
// commented out because currently this would prevent the test project from compiling
// because we generate both abstract classes _and_ dummy implementations
serverOptions.withGenerateImplementationFiles(false)
builder.forKotlinServer(serverOptions ->
serverOptions
.withControllerPackage("io.micronaut.openapi.test.controller")
// commented out because currently this would prevent the test project from compiling
// because we generate both abstract classes _and_ dummy implementations
.withGenerateImplementationFiles(false)
.withAuthentication(false)
.withKsp(ksp)
.withGeneratedAnnotation(generatedAnnotation);
});
.withGeneratedAnnotation(generatedAnnotation)
);
} else {
builder.forJavaServer(serverOptions -> {
serverOptions.withControllerPackage("io.micronaut.openapi.test.controller");
// commented out because currently this would prevent the test project from compiling
// because we generate both abstract classes _and_ dummy implementations
serverOptions.withGenerateImplementationFiles(false)
builder.forJavaServer(serverOptions ->
serverOptions
.withControllerPackage("io.micronaut.openapi.test.controller")
// commented out because currently this would prevent the test project from compiling
// because we generate both abstract classes _and_ dummy implementations
.withGenerateImplementationFiles(false)
.withAuthentication(false)
.withGeneratedAnnotation(generatedAnnotation);
});
.withGeneratedAnnotation(generatedAnnotation)
);
}
} else {
if (lang == GeneratorLanguage.KOTLIN) {
builder.forKotlinClient(client -> {
client.withGeneratedAnnotation(generatedAnnotation)
.withKsp(ksp);
});
builder.forKotlinClient(clientOptions ->
clientOptions
.withGeneratedAnnotation(generatedAnnotation)
.withKsp(ksp)
.withClientId(clientId)
);
} else {
builder.forJavaClient(client -> {
client.withGeneratedAnnotation(generatedAnnotation);
});
builder.forJavaClient(clientOptions ->
clientOptions
.withGeneratedAnnotation(generatedAnnotation)
.withClientId(clientId)
);
}
}
builder.build().generate();
Expand Down
2 changes: 2 additions & 0 deletions test-suite-java-client-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ dependencies {
runtimeOnly(mnLogging.logback.classic)

testImplementation(mnTest.micronaut.test.spock)

testRuntimeOnly(mn.snakeyaml)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.micronaut.openapi.test.api

import spock.lang.Specification

import static io.micronaut.openapi.test.util.TestUtils.assertFileContains

class ClientSpec extends Specification {

String outputPath = "build/generated/openapi"

void "test client id"() {
expect:
assertFileContains(outputPath + "/src/main/java/io/micronaut/openapi/test/api/PetApi.java",
'@Client(id = "myClient", path = "${openapi-micronaut-client-base-path}")');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package io.micronaut.openapi.test.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* An abstract class with methods useful for testing
*/
public final class TestUtils {

private TestUtils() {
}

public static void assertFileContainsRegex(String path, String... regex) {
assertFileExists(path);
String file = readFile(path);
for (String line : regex) {
assertTrue(Pattern.compile(line.replace(" ", "\\s+")).matcher(file).find());
}
}

public static void assertFileNotContainsRegex(String path, String... regex) {
assertFileExists(path);
String file = readFile(path);
for (String line : regex) {
assertFalse(Pattern.compile(line.replace(" ", "\\s+")).matcher(file).find());
}
}

public static void assertFileContains(String path, String... lines) {
assertFileExists(path);
String file = linearize(readFile(path));
for (String line : lines) {
assertTrue(file.contains(linearize(line)), "File does not contain line [" + line + "]");
}
}

public static void assertFileNotContains(String path, String... lines) {
assertFileExists(path);
String file = linearize(readFile(path));
for (String line : lines) {
assertFalse(file.contains(linearize(line)), "File contains line [" + line + "]");
}
}

public static void assertFileExists(String file) {
Path path = Paths.get(file);
if (!path.toFile().exists()) {
while (path.getParent() != null && !path.getParent().toFile().exists()) {
path = path.getParent();
}
String message = "File \"" + file + "\" should exist, however \"" + path + "\" could not be found.";
if (path.getParent() != null) {
Path parent = path.getParent();
File[] contents = parent.toFile().listFiles();
message += "\nContents of folder \"" + path.getParent() + "\": ";
if (contents == null) {
message += null;
} else {
message += Arrays.stream(contents)
.map(f -> f.toString().substring(parent.toString().length() + 1))
.toList();
}
message += ".";
}
fail(message);
}
}

public static void assertFileNotExists(String path) {
assertFalse(Paths.get(path).toFile().exists(), "File \"" + path + "\" should not exist");
}

public static String readFile(String path) {
String file = null;
try {
file = Files.readString(Paths.get(path));
assertNotNull(file, "File \"" + path + "\" does not exist");
} catch (IOException e) {
fail("Unable to evaluate file " + path);
}

return file;
}

public static String linearize(String target) {
return target.replaceAll("\r?\n", "").replaceAll("\\s+", "s");
}
}
1 change: 1 addition & 0 deletions test-suite-java-server-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
testRuntimeOnly(mn.micronaut.json.core)
testRuntimeOnly(mnSerde.micronaut.serde.jackson)
testRuntimeOnly(mnLogging.logback.classic)
testRuntimeOnly(mn.snakeyaml)
}

sourceSets {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
micronaut:
server:
context-path: /api
1 change: 1 addition & 0 deletions test-suite-kotlin-kapt-client-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
testRuntimeOnly(mnSerde.micronaut.serde.jackson)
testRuntimeOnly(libs.junit.jupiter.engine)
testRuntimeOnly(mnLogging.logback.classic)
testRuntimeOnly(mn.snakeyaml)
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.micronaut.openapi.test.api

import spock.lang.Specification

import static io.micronaut.openapi.test.util.TestUtils.assertFileContains

class ClientSpec extends Specification {

String outputPath = "build/generated/openapi"

void "test client id"() {
expect:
assertFileContains(outputPath + "/src/main/java/io/micronaut/openapi/test/api/PetApi.java",
'@Client(id = "myClient", path = "${openapi-micronaut-client-base-path}")');
}
}
Loading
Loading