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 1 commit
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 @@ -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
148 changes: 148 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_15933.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
openapi: 3.1.0
info:
title: TEST
description: |-
## TEST
version: 1.0.0

servers:
- url: /v3
description: Major version of service

tags:
- name: pet tag
description: Everything about your Pets
paths:
/pet:
post:
tags:
- pet tag
summary: Add a new pet to the store
description: ''
operationId: addPet
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
'405':
description: Invalid input
put:
tags:
- pet tag
summary: Update an existing pet
description: ''
operationId: updatePet
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
'400':
description: Invalid ID supplied
'404':
description: Pet not found
'405':
description: Validation exception
/pet/findByStatus:
get:
tags:
- pet tag
summary: Finds Pets by status
description: Multiple status values can be provided with comma separated strings
operationId: findPetsByStatus
parameters:
- name: status
in: query
description: Status values that need to be considered for filter
required: true
style: form
explode: false
deprecated: true
schema:
type: array
items:
type: string
enum:
- available
- pending
- sold
default: available
responses:
'200':
description: successful operation
content:
application/xml:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
'400':
'/pet/{petId}':
get:
tags:
- pet tag
summary: Find pet by ID
description: Returns a single pet
operationId: getPetById
parameters:
- name: petId
in: path
description: ID of pet to return
required: true
schema:
type: integer
format: int64
responses:
'200':
description: successful operation
content:
application/xml:
schema:
$ref: '#/components/schemas/Pet'
application/json:
schema:
$ref: '#/components/schemas/Pet'
'400':
description: Invalid ID supplied
'404':
description: Pet not found
security:
- api_key: []
post:
tags:
- pet tag
summary: Updates a pet in the store with form data
description: ''
operationId: updatePetWithForm
parameters:
- name: petId
in: path
description: ID of pet that needs to be updated
required: true
schema:
type: integer
format: int64
responses:
'405':
description: Invalid input
components:
Pet:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
schema:
$ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store
required: true
Loading