Skip to content

Commit

Permalink
Fix missing model Prefix and api prefix/suffix handling, fix #414 (#415)
Browse files Browse the repository at this point in the history
* Add testcase for api/model suffix/prefix

* Fix prefix/suffix
  • Loading branch information
sschnabe authored Sep 18, 2024
1 parent 5dcdd30 commit a8f4e77
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 19 deletions.
9 changes: 9 additions & 0 deletions gen/main/java/issue/_414/ApiPrefixDefaultApiSuffix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package issue._414;

@jakarta.annotation.Generated("org.openapitools.codegen.languages.MicronautCodegen")
@io.micronaut.validation.Validated
public interface ApiPrefixDefaultApiSuffix {

@io.micronaut.http.annotation.Get("/doIt")
io.micronaut.http.HttpResponse<Object> doIt();
}
34 changes: 34 additions & 0 deletions gen/main/java/issue/_414/ModelPrefixModelEnumModelSuffix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package issue._414;

@jakarta.annotation.Generated("org.openapitools.codegen.languages.MicronautCodegen")
public enum ModelPrefixModelEnumModelSuffix {

FOO("foo"),
BAR("bar");

public static final java.lang.String FOO_VALUE = "foo";
public static final java.lang.String BAR_VALUE = "bar";

private final java.lang.String value;

private ModelPrefixModelEnumModelSuffix(java.lang.String value) {
this.value = value;
}

@com.fasterxml.jackson.annotation.JsonCreator
public static ModelPrefixModelEnumModelSuffix toEnum(java.lang.String value) {
return toOptional(value).orElseThrow(() -> new IllegalArgumentException("Unknown value '" + value + "'."));
}

public static java.util.Optional<ModelPrefixModelEnumModelSuffix> toOptional(java.lang.String value) {
return java.util.Arrays
.stream(values())
.filter(e -> e.value.equals(value))
.findAny();
}

@com.fasterxml.jackson.annotation.JsonValue
public java.lang.String getValue() {
return value;
}
}
55 changes: 55 additions & 0 deletions gen/main/java/issue/_414/ModelPrefixModelObjectModelSuffix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package issue._414;

@jakarta.annotation.Generated("org.openapitools.codegen.languages.MicronautCodegen")
@io.micronaut.serde.annotation.Serdeable
public class ModelPrefixModelObjectModelSuffix {

@com.fasterxml.jackson.annotation.JsonProperty("a")
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
private java.lang.String a;

// methods

@Override
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
ModelPrefixModelObjectModelSuffix other = (ModelPrefixModelObjectModelSuffix) object;
return java.util.Objects.equals(a, other.a);
}

@Override
public int hashCode() {
return java.util.Objects.hash(a);
}

@Override
public java.lang.String toString() {
return new java.lang.StringBuilder()
.append("ModelPrefixModelObjectModelSuffix[")
.append("a=").append(a)
.append("]")
.toString();
}

// fluent

public ModelPrefixModelObjectModelSuffix a(java.lang.String newA) {
this.a = newA;
return this;
}

// getter/setter

public java.lang.String getA() {
return a;
}

public void setA(java.lang.String newA) {
this.a = newA;
}
}
10 changes: 10 additions & 0 deletions gen/test/java/issue/_414/ApiPrefixDefaultApiSuffixTestClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package issue._414;

/** Test client for {@link ApiPrefixDefaultApiSuffix}. **/
@jakarta.annotation.Generated("org.openapitools.codegen.languages.MicronautCodegen")
@io.micronaut.http.client.annotation.Client("${micronaut.http.services.test.clientId:/}")
public interface ApiPrefixDefaultApiSuffixTestClient {

@io.micronaut.http.annotation.Get("/doIt")
io.micronaut.http.HttpResponse<?> doIt();
}
10 changes: 10 additions & 0 deletions gen/test/java/issue/_414/ApiPrefixDefaultApiSuffixTestSpec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package issue._414;

/** Test for {@link ApiPrefixDefaultApiSuffix}. */
@jakarta.annotation.Generated("org.openapitools.codegen.languages.MicronautCodegen")
public interface ApiPrefixDefaultApiSuffixTestSpec {

// doIt

void doIt200() throws java.lang.Exception;
}
65 changes: 65 additions & 0 deletions src/it/issue-414/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@-it</artifactId>
<version>LOCAL-SNAPSHOT</version>
</parent>

<artifactId>@project.artifactId@-it-issue-414</artifactId>

<dependencies>

<!-- micronaut -->
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
</dependency>

<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>

<!-- test -->
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>

<!-- generate sources from openapi spec -->
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<configuration>
<inputSpec>@project.basedir@/src/test/resources/openapi/issue-414.yaml</inputSpec>
<generateSupportingFiles>false</generateSupportingFiles>
<apiNameSuffix>ApiSuffix</apiNameSuffix>
<modelNamePrefix>ModelPrefix</modelNamePrefix>
<modelNameSuffix>ModelSuffix</modelNameSuffix>
<configOptions>
<apiNamePrefix>ApiPrefix</apiNamePrefix>
<generateConstants>false</generateConstants>
<openApiNullable>false</openApiNullable>
<useBeanValidation>false</useBeanValidation>
<useGenericResponse>false</useGenericResponse>
<serdeable>false</serdeable>
</configOptions>
<typeMappings>
<typeMapping>Generated=</typeMapping>
</typeMappings>
</configuration>
</plugin>

</plugins>
</build>
</project>
21 changes: 21 additions & 0 deletions src/it/issue-414/src/test/java/codegen/GeneratedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package codegen;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import org.junit.jupiter.api.Test;

public class GeneratedTest {

@Test
void load() {
assertClass("codegen.ModelPrefixModelObjectModelSuffix");
assertClass("codegen.ModelPrefixModelEnumModelSuffix");
assertClass("codegen.server.ApiPrefixDefaultApiSuffix");
assertClass("codegen.server.ApiPrefixDefaultApiSuffixTestClient");
assertClass("codegen.server.ApiPrefixDefaultApiSuffixTestSpec");
}

private static void assertClass(String name) {
assertDoesNotThrow(() -> Class.forName(name), "Failed to load " + name);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.openapitools.codegen.languages;

import static org.openapitools.codegen.CodegenConstants.API_NAME_PREFIX;
import static org.openapitools.codegen.CodegenConstants.API_NAME_PREFIX_DESC;
import static org.openapitools.codegen.CodegenConstants.API_NAME_SUFFIX;
import static org.openapitools.codegen.CodegenConstants.API_NAME_SUFFIX_DESC;
import static org.openapitools.codegen.CodegenConstants.GENERATE_API_TESTS;
import static org.openapitools.codegen.CodegenConstants.MODEL_NAME_PREFIX;
import static org.openapitools.codegen.CodegenConstants.MODEL_NAME_PREFIX_DESC;
import static org.openapitools.codegen.CodegenConstants.MODEL_NAME_SUFFIX;
import static org.openapitools.codegen.CodegenConstants.MODEL_NAME_SUFFIX_DESC;
import static org.openapitools.codegen.CodegenConstants.REMOVE_ENUM_VALUE_PREFIX;
import static org.openapitools.codegen.CodegenConstants.REMOVE_ENUM_VALUE_PREFIX_DESC;
import static org.openapitools.codegen.CodegenConstants.SOURCE_FOLDER;
Expand Down Expand Up @@ -104,6 +111,10 @@ public MicronautCodegen() {
cliOptions.add(CliOption.newString(CLIENT_ID, "ClientId to use."));
cliOptions.add(CliOption.newString(SOURCE_FOLDER, SOURCE_FOLDER_DESC));
cliOptions.add(CliOption.newString("testFolder", "test folder for generated code"));
cliOptions.add(CliOption.newString(API_NAME_PREFIX, API_NAME_PREFIX_DESC));
cliOptions.add(CliOption.newString(API_NAME_SUFFIX, API_NAME_SUFFIX_DESC));
cliOptions.add(CliOption.newString(MODEL_NAME_PREFIX, MODEL_NAME_PREFIX_DESC));
cliOptions.add(CliOption.newString(MODEL_NAME_SUFFIX, MODEL_NAME_SUFFIX_DESC));
cliOptions.add(
CliOption.newBoolean(REMOVE_ENUM_VALUE_PREFIX, REMOVE_ENUM_VALUE_PREFIX_DESC, removeEnumValuePrefix));

Expand Down Expand Up @@ -197,6 +208,8 @@ public void postProcess() {}

@Override
public void processOpts() {
super.useCodegenAsMustacheParentContext();

BiFunction<String, String, String> getOrDefault = (key,
defaultValue) -> (String) additionalProperties.computeIfAbsent(key, k -> defaultValue);

Expand All @@ -210,15 +223,6 @@ public void processOpts() {

// process flags - this class

if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
useBeanValidation = convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION);
}
if (additionalProperties.containsKey(USE_GENERIC_RESPONSE)) {
useGenericResponse = convertPropertyToBooleanAndWriteBack(USE_GENERIC_RESPONSE);
}
if (additionalProperties.containsKey(USE_OPTIONAL)) {
useOptional = convertPropertyToBooleanAndWriteBack(USE_OPTIONAL);
}
if (additionalProperties.containsKey(SERDEABLE)) {
serdeable = convertPropertyToBooleanAndWriteBack(SERDEABLE);
}
Expand All @@ -234,15 +238,9 @@ public void processOpts() {
if (additionalProperties.containsKey(RECORD)) {
record = convertPropertyToBooleanAndWriteBack(RECORD);
}
if (additionalProperties.containsKey(SUPPORT_ASYNC)) {
supportAsync = convertPropertyToBooleanAndWriteBack(SUPPORT_ASYNC);
}
if (additionalProperties.containsKey(GENERATE_API_TESTS)) {
generateApiTests = convertPropertyToBooleanAndWriteBack(GENERATE_API_TESTS);
}
if (additionalProperties.containsKey(OPENAPI_NULLABLE)) {
openApiNullable = convertPropertyToBooleanAndWriteBack(OPENAPI_NULLABLE);
}
if (additionalProperties.containsKey(GENERATE_AUTHENTICATION)) {
generateAuthentication = convertPropertyToBooleanAndWriteBack(GENERATE_AUTHENTICATION);
}
Expand All @@ -252,9 +250,19 @@ record = convertPropertyToBooleanAndWriteBack(RECORD);
if (additionalProperties.containsKey(GENERATE_CONSTANTS)) {
generateConstants = convertPropertyToBooleanAndWriteBack(GENERATE_CONSTANTS);
}
if (additionalProperties.containsKey(REMOVE_ENUM_VALUE_PREFIX)) {
removeEnumValuePrefix = convertPropertyToBooleanAndWriteBack(REMOVE_ENUM_VALUE_PREFIX);
}

// generic properties

convertPropertyToStringAndWriteBack(API_NAME_PREFIX, this::setApiNamePrefix);
convertPropertyToStringAndWriteBack(API_NAME_SUFFIX, this::setApiNameSuffix);
convertPropertyToStringAndWriteBack(MODEL_NAME_PREFIX, this::setModelNamePrefix);
convertPropertyToStringAndWriteBack(MODEL_NAME_SUFFIX, this::setModelNameSuffix);
convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION, this::setUseBeanValidation);
convertPropertyToBooleanAndWriteBack(USE_GENERIC_RESPONSE, this::setUseGenericResponse);
convertPropertyToBooleanAndWriteBack(USE_OPTIONAL, this::setUseOptional);
convertPropertyToBooleanAndWriteBack(REMOVE_ENUM_VALUE_PREFIX, this::setRemoveEnumValuePrefix);
convertPropertyToBooleanAndWriteBack(OPENAPI_NULLABLE, this::setOpenApiNullable);
convertPropertyToBooleanAndWriteBack(SUPPORT_ASYNC, this::setSupportAsync);

// we do not generate projects, only api, set source and test folder

Expand All @@ -263,7 +271,6 @@ record = convertPropertyToBooleanAndWriteBack(RECORD);
projectTestFolder = getOrDefault.apply("projectTestFolder", "generated-test-sources");
sourceFolder = getOrDefault.apply(SOURCE_FOLDER, projectFolder + File.separator + "openapi");
testFolder = getOrDefault.apply("testFolder", projectTestFolder + File.separator + "openapi");
modelNameSuffix = getOrDefault.apply(MODEL_NAME_SUFFIX, modelNameSuffix);

// add files to generate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ void apiWithParametersWithOptionalAndValidation() {
generate(configurator("src/test/resources/openapi/issue-372.yaml", "issue._372"));
}

@DisplayName("model with prefix")
@Test
void modelWithPrefix() {
generate(configurator("src/test/resources/openapi/issue-414.yaml", "issue._414")
.addAdditionalProperty(CodegenConstants.API_NAME_PREFIX, "ApiPrefix")
.addAdditionalProperty(CodegenConstants.API_NAME_SUFFIX, "ApiSuffix")
.addAdditionalProperty(CodegenConstants.MODEL_NAME_PREFIX, "ModelPrefix")
.addAdditionalProperty(CodegenConstants.MODEL_NAME_SUFFIX, "ModelSuffix"));
}

static void generate(CodegenConfigurator configurator) {
var gen = new DefaultGenerator();
gen.setGenerateMetadata(false);
Expand Down
21 changes: 21 additions & 0 deletions src/test/resources/openapi/issue-414.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.0.3
info:
title: Spec for <https://github.com/kokuwaio/micronaut-openapi-codegen/issues/414>
version: "1"
paths:
/doIt:
get:
operationId: doIt
responses:
200:
description: OK
components:
schemas:
ModelObject:
type: object
properties:
a:
type: string
ModelEnum:
type: string
enum: [foo, bar]

0 comments on commit a8f4e77

Please sign in to comment.