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 cli options use tags and api name suffix #15936

Merged
merged 5 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion bin/configs/spring-cloud-date-time-oas3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ additionalProperties:
artifactId: spring-cloud-date-time-oas3
interfaceOnly: "true"
singleContentTypes: "true"
hideGenerationTimestamp: "true"
hideGenerationTimestamp: "true"
useTags: "true"
3 changes: 2 additions & 1 deletion bin/configs/spring-cloud-date-time.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ additionalProperties:
artifactId: spring-cloud-date-time
interfaceOnly: "true"
singleContentTypes: "true"
hideGenerationTimestamp: "true"
hideGenerationTimestamp: "true"
useTags: "true"
1 change: 1 addition & 0 deletions bin/configs/spring-cloud-oas3-fakeapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ additionalProperties:
singleContentTypes: "true"
hideGenerationTimestamp: "true"
generatedConstructorWithRequiredArgs: "false"
useTags: "true"
1 change: 1 addition & 0 deletions bin/configs/spring-cloud-oas3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ additionalProperties:
interfaceOnly: "true"
singleContentTypes: "true"
hideGenerationTimestamp: "true"
useTags: "true"
13 changes: 13 additions & 0 deletions bin/configs/spring-cloud-tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generatorName: spring
library: spring-cloud
outputDir: samples/client/petstore/spring-cloud-tags
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/petstore-with-tags.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
documentationProvider: springfox
artifactId: spring-cloud-date-time
interfaceOnly: "true"
singleContentTypes: "true"
hideGenerationTimestamp: "true"
useTags: "false"
apiNameSuffix: "Controller"
Original file line number Diff line number Diff line change
Expand Up @@ -724,10 +724,14 @@ private boolean containsEnums() {
.anyMatch(it -> it.getEnum() != null && !it.getEnum().isEmpty());
}

private boolean supportLibraryUseTags(){
return SPRING_BOOT.equals(library) || SPRING_CLOUD_LIBRARY.equals(library);
}

@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co,
Map<String, List<CodegenOperation>> operations) {
if ((SPRING_BOOT.equals(library) && !useTags)) {
if (supportLibraryUseTags() && !useTags) {
String basePath = resourcePath;
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
Expand All @@ -745,9 +749,10 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
final List<CodegenOperation> opList = operations.computeIfAbsent(basePath, k -> new ArrayList<>());
opList.add(co);
co.baseName = basePath;
} else {
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
return;
}
super.addOperationToGroup(tag, resourcePath, operation, co, operations);

}

@Override
Expand Down Expand Up @@ -958,7 +963,7 @@ public String toApiName(String name) {
return "DefaultApi";
}
name = sanitizeName(name);
return camelize(name) + "Api";
return camelize(name) + apiNameSuffix;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This will impact all spring code gen, not just cloud.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but the default is "Api", correct? So nothing should happen for the other ones. The tests said it's okay :P

I think it make sense also for the other spring generator to add a name suffix if you want. Nothing special to spring-cloud

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,101 @@ public void shouldAddParameterWithInHeaderWhenImplicitHeadersIsTrue_issue14418()
));
}

@Test
public void shouldApiNameSuffixForApiClassname() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_1/petstore.yaml", null, new ParseOptions()).getOpenAPI();
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_CLOUD_LIBRARY);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.model");
codegen.additionalProperties().put(CodegenConstants.API_NAME_SUFFIX, "Controller");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.controller");
codegen.additionalProperties().put(CodegenConstants.MODEL_NAME_SUFFIX, "Dto");


ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

JavaFileAssert.assertThat(files.get("PetController.java"))
.isInterface();

File notExisting = files.get("PetApi.java");
assertThat(notExisting).isNull();

}
@Test
public void shouldUseTagsForClassname() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_15933.yaml", null, new ParseOptions()).getOpenAPI();
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_CLOUD_LIBRARY);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(USE_TAGS, "true");
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.model");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.controller");
codegen.additionalProperties().put(CodegenConstants.MODEL_NAME_SUFFIX, "Dto");


ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

JavaFileAssert.assertThat(files.get("PetTagApi.java"))
.isInterface();

File notExisting = files.get("PetApi.java");
assertThat(notExisting).isNull();

}

@Test
public void shouldNotUseTagsForClassname() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_15933.yaml", null, new ParseOptions()).getOpenAPI();
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_CLOUD_LIBRARY);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(USE_TAGS, "false");
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.model");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.controller");
codegen.additionalProperties().put(CodegenConstants.MODEL_NAME_SUFFIX, "Dto");



ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

JavaFileAssert.assertThat(files.get("PetApi.java"))
.isInterface();

File notExisting = files.get("PetTagApi.java");
assertThat(notExisting).isNull();
}

@Test
public void shouldAddValidAnnotationIntoCollectionWhenBeanValidationIsEnabled_issue14723() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down Expand Up @@ -2126,6 +2221,7 @@ public void requiredFieldShouldIncludeNotNullAnnotationWithBeanValidationTrue_is
.hasImports("javax.validation.constraints");
}

@Test
public void shouldUseEqualsNullableForArrayWhenSetInConfig_issue13385() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
Expand Down
Loading
Loading