Skip to content

Commit c5d67ee

Browse files
authored
Fix #11570 by ensuring tags at the class level match tags at the method level (#13434)
1 parent e9f55c0 commit c5d67ee

74 files changed

Lines changed: 132 additions & 73 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.regex.Matcher;
4545
import java.util.stream.Collectors;
4646

47+
import io.swagger.v3.oas.models.tags.Tag;
4748
import org.apache.commons.lang3.tuple.Pair;
4849
import org.openapitools.codegen.CliOption;
4950
import org.openapitools.codegen.CodegenConstants;
@@ -802,7 +803,13 @@ public void setReturnContainer(final String returnContainer) {
802803

803804
handleImplicitHeaders(operation);
804805
}
805-
objs.put("tagDescription", ops.get(0).tags.get(0).getDescription());
806+
// The tag for the controller is the first tag of the first operation
807+
final CodegenOperation firstOperation = ops.get(0);
808+
final Tag firstTag = firstOperation.tags.get(0);
809+
final String firstTagName = firstTag.getName();
810+
// But use a sensible tag name if there is none
811+
objs.put("tagName", "default".equals(firstTagName) ? firstOperation.baseName : firstTagName);
812+
objs.put("tagDescription", firstTag.getDescription());
806813
}
807814

808815
return objs;

modules/openapi-generator/src/main/resources/JavaSpring/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ import {{javaxPackage}}.annotation.Generated;
7979
@Controller
8080
{{/useSpringController}}
8181
{{#swagger2AnnotationLibrary}}
82-
@Tag(name = "{{{baseName}}}", description = {{#tagDescription}}"{{{.}}}"{{/tagDescription}}{{^tagDescription}}"the {{{baseName}}} API"{{/tagDescription}})
82+
@Tag(name = "{{{tagName}}}", description = {{#tagDescription}}"{{{.}}}"{{/tagDescription}}{{^tagDescription}}"the {{{tagName}}} API"{{/tagDescription}})
8383
{{/swagger2AnnotationLibrary}}
8484
{{#swagger1AnnotationLibrary}}
85-
@Api(value = "{{{baseName}}}", description = {{#tagDescription}}"{{{.}}}"{{/tagDescription}}{{^tagDescription}}"the {{{baseName}}} API"{{/tagDescription}})
85+
@Api(value = "{{{tagName}}}", description = {{#tagDescription}}"{{{.}}}"{{/tagDescription}}{{^tagDescription}}"the {{{tagName}}} API"{{/tagDescription}})
8686
{{/swagger1AnnotationLibrary}}
8787
{{#operations}}
8888
{{#virtualService}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,18 @@ public void contractWithEnumContainsEnumConverter() throws IOException {
18961896
.assertMethod("typeConverter");
18971897
}
18981898

1899+
@Test
1900+
public void shouldUseTheSameTagNameForTheInterfaceAndTheMethod_issue11570() throws IOException {
1901+
final Map<String, File> output = generateFromContract("src/test/resources/bugs/issue_11570.yml", SPRING_BOOT);
1902+
1903+
final String expectedTagName = "\"personTagWithExclamation!\"";
1904+
final String expectedTagDescription = "\"the personTagWithExclamation! API\"";
1905+
1906+
final String interfaceTag = "@Tag(name = " + expectedTagName + ", description = " + expectedTagDescription + ")";
1907+
final String methodTag = "tags = { " + expectedTagName + " }";
1908+
assertFileContains(output.get("PersonApi.java").toPath(), interfaceTag, methodTag);
1909+
}
1910+
18991911
private Map<String, File> generateFromContract(String url, String library) throws IOException {
19001912
return generateFromContract(url, library, new HashMap<>());
19011913
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
openapi: 3.0.0
2+
info:
3+
description: Specification to reproduce issue 11570 where the wrong tags are applied to the controller
4+
title: Required Api
5+
paths:
6+
'/person':
7+
post:
8+
summary: Inserts a person
9+
operationId: postPerson
10+
tags:
11+
- personTagWithExclamation!
12+
requestBody:
13+
required: true
14+
content:
15+
application/json:
16+
schema:
17+
$ref: '#/components/schemas/Person'
18+
19+
responses: ...
20+
21+
components:
22+
schemas:
23+
Person:
24+
type: object
25+
required:
26+
- name
27+
properties:
28+
name:
29+
type: string
30+
maxLength: 50
31+
format: email
32+
id:
33+
type: integer
34+
Alien:
35+
type: object
36+
properties:
37+
name:
38+
type: string
39+
id:
40+
type: integer

samples/client/petstore/spring-cloud-async/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
2828
@Validated
29-
@Api(value = "Pet", description = "Everything about your Pets")
29+
@Api(value = "pet", description = "Everything about your Pets")
3030
public interface PetApi {
3131

3232
/**

samples/client/petstore/spring-cloud-async/src/main/java/org/openapitools/api/StoreApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
2828
@Validated
29-
@Api(value = "Store", description = "Access to Petstore orders")
29+
@Api(value = "store", description = "Access to Petstore orders")
3030
public interface StoreApi {
3131

3232
/**

samples/client/petstore/spring-cloud-async/src/main/java/org/openapitools/api/UserApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
2929
@Validated
30-
@Api(value = "User", description = "Operations about user")
30+
@Api(value = "user", description = "Operations about user")
3131
public interface UserApi {
3232

3333
/**

samples/client/petstore/spring-cloud-feign-without-url/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
2727
@Validated
28-
@Api(value = "Pet", description = "Everything about your Pets")
28+
@Api(value = "pet", description = "Everything about your Pets")
2929
public interface PetApi {
3030

3131
/**

samples/client/petstore/spring-cloud-feign-without-url/src/main/java/org/openapitools/api/StoreApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
2727
@Validated
28-
@Api(value = "Store", description = "Access to Petstore orders")
28+
@Api(value = "store", description = "Access to Petstore orders")
2929
public interface StoreApi {
3030

3131
/**

samples/client/petstore/spring-cloud-feign-without-url/src/main/java/org/openapitools/api/UserApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
2828
@Validated
29-
@Api(value = "User", description = "Operations about user")
29+
@Api(value = "user", description = "Operations about user")
3030
public interface UserApi {
3131

3232
/**

0 commit comments

Comments
 (0)