Skip to content

Commit

Permalink
Fix cli options use tags and api name suffix (#15936)
Browse files Browse the repository at this point in the history
* Fix cli options use tags and api name suffix

* Add sample code generator for api suffix and tags

* Fix FILES

* trigger build

* Add sample to github workflow
  • Loading branch information
MelleD committed Jul 2, 2023
1 parent a22a7c0 commit 50d8e78
Show file tree
Hide file tree
Showing 24 changed files with 3,002 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/samples-spring.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
- samples/server/petstore/springboot-spring-pageable-delegatePattern
- samples/server/petstore/springboot-spring-pageable-without-j8
- samples/server/petstore/springboot-spring-pageable
- samples/openapi3/client/petstore/spring-cloud-tags.yaml
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
Expand Down
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,13 @@ 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)) {
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
if (supportLibraryUseTags() && !useTags) {
String basePath = resourcePath;
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
Expand All @@ -745,9 +748,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 +962,7 @@ public String toApiName(String name) {
return "DefaultApi";
}
name = sanitizeName(name);
return camelize(name) + "Api";
return camelize(name) + apiNameSuffix;
}

@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

0 comments on commit 50d8e78

Please sign in to comment.