Skip to content

Commit cfe476f

Browse files
authored
immutable Pair, better performant HttpBearerAuth, consistent code-style (#20743)
* make Pair immutable and in google-code-style * apply google-code-style to JavaTimeFormatter (to make it consistent with most other auto-generated java) * move upperCaseBearer to ctor (scheme is final and private; only needs to be fixed once); also replaced Optional with ternary (perf and cleaner code) * apply google-code-style to Authentication to make it consistent with rest of auth code * fresh samples
1 parent d911a71 commit cfe476f

File tree

87 files changed

+2120
-2738
lines changed

Some content is hidden

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

87 files changed

+2120
-2738
lines changed

modules/openapi-generator/src/main/resources/Java/JavaTimeFormatter.mustache

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,47 @@ import java.time.format.DateTimeParseException;
1111
*/
1212
{{>generatedAnnotation}}
1313
public class JavaTimeFormatter {
14+
private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
1415
15-
private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
16+
/**
17+
* Get the date format used to parse/format {@code OffsetDateTime} parameters.
18+
*
19+
* @return DateTimeFormatter
20+
*/
21+
public DateTimeFormatter getOffsetDateTimeFormatter() {
22+
return offsetDateTimeFormatter;
23+
}
1624

17-
/**
18-
* Get the date format used to parse/format {@code OffsetDateTime} parameters.
19-
* @return DateTimeFormatter
20-
*/
21-
public DateTimeFormatter getOffsetDateTimeFormatter() {
22-
return offsetDateTimeFormatter;
23-
}
25+
/**
26+
* Set the date format used to parse/format {@code OffsetDateTime} parameters.
27+
*
28+
* @param offsetDateTimeFormatter {@code DateTimeFormatter}
29+
*/
30+
public void setOffsetDateTimeFormatter(DateTimeFormatter offsetDateTimeFormatter) {
31+
this.offsetDateTimeFormatter = offsetDateTimeFormatter;
32+
}
2433

25-
/**
26-
* Set the date format used to parse/format {@code OffsetDateTime} parameters.
27-
* @param offsetDateTimeFormatter {@code DateTimeFormatter}
28-
*/
29-
public void setOffsetDateTimeFormatter(DateTimeFormatter offsetDateTimeFormatter) {
30-
this.offsetDateTimeFormatter = offsetDateTimeFormatter;
34+
/**
35+
* Parse the given string into {@code OffsetDateTime} object.
36+
*
37+
* @param str String
38+
* @return {@code OffsetDateTime}
39+
*/
40+
public OffsetDateTime parseOffsetDateTime(String str) {
41+
try {
42+
return OffsetDateTime.parse(str, offsetDateTimeFormatter);
43+
} catch (DateTimeParseException e) {
44+
throw new RuntimeException(e);
3145
}
46+
}
3247

33-
/**
34-
* Parse the given string into {@code OffsetDateTime} object.
35-
* @param str String
36-
* @return {@code OffsetDateTime}
37-
*/
38-
public OffsetDateTime parseOffsetDateTime(String str) {
39-
try {
40-
return OffsetDateTime.parse(str, offsetDateTimeFormatter);
41-
} catch (DateTimeParseException e) {
42-
throw new RuntimeException(e);
43-
}
44-
}
45-
/**
46-
* Format the given {@code OffsetDateTime} object into string.
47-
* @param offsetDateTime {@code OffsetDateTime}
48-
* @return {@code OffsetDateTime} in string format
49-
*/
50-
public String formatOffsetDateTime(OffsetDateTime offsetDateTime) {
51-
return offsetDateTimeFormatter.format(offsetDateTime);
52-
}
53-
}
48+
/**
49+
* Format the given {@code OffsetDateTime} object into string.
50+
*
51+
* @param offsetDateTime {@code OffsetDateTime}
52+
* @return {@code OffsetDateTime} in string format
53+
*/
54+
public String formatOffsetDateTime(OffsetDateTime offsetDateTime) {
55+
return offsetDateTimeFormatter.format(offsetDateTime);
56+
}
57+
}

modules/openapi-generator/src/main/resources/Java/Pair.mustache

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,23 @@ package {{invokerPackage}};
44

55
{{>generatedAnnotation}}
66
public class Pair {
7-
private String name = "";
8-
private String value = "";
7+
private final String name;
8+
private final String value;
99
10-
public Pair (String name, String value) {
11-
setName(name);
12-
setValue(value);
13-
}
10+
public Pair(String name, String value) {
11+
this.name = isValidString(name) ? name : "";
12+
this.value = isValidString(value) ? value : "";
13+
}
1414

15-
private void setName(String name) {
16-
if (!isValidString(name)) {
17-
return;
18-
}
15+
public String getName() {
16+
return this.name;
17+
}
1918

20-
this.name = name;
21-
}
19+
public String getValue() {
20+
return this.value;
21+
}
2222

23-
private void setValue(String value) {
24-
if (!isValidString(value)) {
25-
return;
26-
}
27-
28-
this.value = value;
29-
}
30-
31-
public String getName() {
32-
return this.name;
33-
}
34-
35-
public String getValue() {
36-
return this.value;
37-
}
38-
39-
private boolean isValidString(String arg) {
40-
if (arg == null) {
41-
return false;
42-
}
43-
44-
return true;
45-
}
23+
private static boolean isValidString(String arg) {
24+
return arg != null;
25+
}
4626
}

modules/openapi-generator/src/main/resources/Java/auth/Authentication.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import java.util.List;
99

1010
{{>generatedAnnotation}}
1111
public interface Authentication {
12-
/**
13-
* Apply authentication settings to header and query params.
14-
*
15-
* @param queryParams List of query parameters
16-
* @param headerParams Map of header parameters
17-
* @param cookieParams Map of cookie parameters
18-
*/
19-
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams);
12+
/**
13+
* Apply authentication settings to header and query params.
14+
*
15+
* @param queryParams List of query parameters
16+
* @param headerParams Map of header parameters
17+
* @param cookieParams Map of cookie parameters
18+
*/
19+
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams);
2020
}

modules/openapi-generator/src/main/resources/Java/auth/HttpBearerAuth.mustache

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {{invokerPackage}}.Pair;
66

77
import java.util.List;
88
import java.util.Map;
9-
import java.util.Optional;
109
import java.util.function.Supplier;
1110

1211
{{>generatedAnnotation}}
@@ -15,7 +14,7 @@ public class HttpBearerAuth implements Authentication {
1514
private Supplier<String> tokenSupplier;
1615
1716
public HttpBearerAuth(String scheme) {
18-
this.scheme = scheme;
17+
this.scheme = upperCaseBearer(scheme);
1918
}
2019

2120
/**
@@ -47,15 +46,14 @@ public class HttpBearerAuth implements Authentication {
4746

4847
@Override
4948
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
50-
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
49+
String bearerToken = tokenSupplier != null ? tokenSupplier.get() : null;
5150
if (bearerToken == null) {
5251
return;
5352
}
54-
55-
headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken);
53+
headerParams.put("Authorization", (scheme != null ? scheme + " " : "") + bearerToken);
5654
}
5755

5856
private static String upperCaseBearer(String scheme) {
59-
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme;
57+
return "bearer".equalsIgnoreCase(scheme) ? "Bearer" : scheme;
6058
}
6159
}

samples/client/echo_api/java/apache-httpclient/src/main/java/org/openapitools/client/JavaTimeFormatter.java

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,47 @@
2222
*/
2323
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.15.0-SNAPSHOT")
2424
public class JavaTimeFormatter {
25+
private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
2526

26-
private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
27+
/**
28+
* Get the date format used to parse/format {@code OffsetDateTime} parameters.
29+
*
30+
* @return DateTimeFormatter
31+
*/
32+
public DateTimeFormatter getOffsetDateTimeFormatter() {
33+
return offsetDateTimeFormatter;
34+
}
2735

28-
/**
29-
* Get the date format used to parse/format {@code OffsetDateTime} parameters.
30-
* @return DateTimeFormatter
31-
*/
32-
public DateTimeFormatter getOffsetDateTimeFormatter() {
33-
return offsetDateTimeFormatter;
34-
}
36+
/**
37+
* Set the date format used to parse/format {@code OffsetDateTime} parameters.
38+
*
39+
* @param offsetDateTimeFormatter {@code DateTimeFormatter}
40+
*/
41+
public void setOffsetDateTimeFormatter(DateTimeFormatter offsetDateTimeFormatter) {
42+
this.offsetDateTimeFormatter = offsetDateTimeFormatter;
43+
}
3544

36-
/**
37-
* Set the date format used to parse/format {@code OffsetDateTime} parameters.
38-
* @param offsetDateTimeFormatter {@code DateTimeFormatter}
39-
*/
40-
public void setOffsetDateTimeFormatter(DateTimeFormatter offsetDateTimeFormatter) {
41-
this.offsetDateTimeFormatter = offsetDateTimeFormatter;
45+
/**
46+
* Parse the given string into {@code OffsetDateTime} object.
47+
*
48+
* @param str String
49+
* @return {@code OffsetDateTime}
50+
*/
51+
public OffsetDateTime parseOffsetDateTime(String str) {
52+
try {
53+
return OffsetDateTime.parse(str, offsetDateTimeFormatter);
54+
} catch (DateTimeParseException e) {
55+
throw new RuntimeException(e);
4256
}
57+
}
4358

44-
/**
45-
* Parse the given string into {@code OffsetDateTime} object.
46-
* @param str String
47-
* @return {@code OffsetDateTime}
48-
*/
49-
public OffsetDateTime parseOffsetDateTime(String str) {
50-
try {
51-
return OffsetDateTime.parse(str, offsetDateTimeFormatter);
52-
} catch (DateTimeParseException e) {
53-
throw new RuntimeException(e);
54-
}
55-
}
56-
/**
57-
* Format the given {@code OffsetDateTime} object into string.
58-
* @param offsetDateTime {@code OffsetDateTime}
59-
* @return {@code OffsetDateTime} in string format
60-
*/
61-
public String formatOffsetDateTime(OffsetDateTime offsetDateTime) {
62-
return offsetDateTimeFormatter.format(offsetDateTime);
63-
}
64-
}
59+
/**
60+
* Format the given {@code OffsetDateTime} object into string.
61+
*
62+
* @param offsetDateTime {@code OffsetDateTime}
63+
* @return {@code OffsetDateTime} in string format
64+
*/
65+
public String formatOffsetDateTime(OffsetDateTime offsetDateTime) {
66+
return offsetDateTimeFormatter.format(offsetDateTime);
67+
}
68+
}

samples/client/echo_api/java/apache-httpclient/src/main/java/org/openapitools/client/Pair.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,23 @@
1515

1616
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.15.0-SNAPSHOT")
1717
public class Pair {
18-
private String name = "";
19-
private String value = "";
18+
private final String name;
19+
private final String value;
2020

21-
public Pair (String name, String value) {
22-
setName(name);
23-
setValue(value);
24-
}
21+
public Pair(String name, String value) {
22+
this.name = isValidString(name) ? name : "";
23+
this.value = isValidString(value) ? value : "";
24+
}
2525

26-
private void setName(String name) {
27-
if (!isValidString(name)) {
28-
return;
29-
}
26+
public String getName() {
27+
return this.name;
28+
}
3029

31-
this.name = name;
32-
}
30+
public String getValue() {
31+
return this.value;
32+
}
3333

34-
private void setValue(String value) {
35-
if (!isValidString(value)) {
36-
return;
37-
}
38-
39-
this.value = value;
40-
}
41-
42-
public String getName() {
43-
return this.name;
44-
}
45-
46-
public String getValue() {
47-
return this.value;
48-
}
49-
50-
private boolean isValidString(String arg) {
51-
if (arg == null) {
52-
return false;
53-
}
54-
55-
return true;
56-
}
34+
private static boolean isValidString(String arg) {
35+
return arg != null;
36+
}
5737
}

samples/client/echo_api/java/apache-httpclient/src/main/java/org/openapitools/client/auth/Authentication.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.15.0-SNAPSHOT")
2222
public interface Authentication {
23-
/**
24-
* Apply authentication settings to header and query params.
25-
*
26-
* @param queryParams List of query parameters
27-
* @param headerParams Map of header parameters
28-
* @param cookieParams Map of cookie parameters
29-
*/
30-
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams);
23+
/**
24+
* Apply authentication settings to header and query params.
25+
*
26+
* @param queryParams List of query parameters
27+
* @param headerParams Map of header parameters
28+
* @param cookieParams Map of cookie parameters
29+
*/
30+
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams);
3131
}

samples/client/echo_api/java/apache-httpclient/src/main/java/org/openapitools/client/auth/HttpBearerAuth.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.List;
1919
import java.util.Map;
20-
import java.util.Optional;
2120
import java.util.function.Supplier;
2221

2322
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.15.0-SNAPSHOT")
@@ -26,7 +25,7 @@ public class HttpBearerAuth implements Authentication {
2625
private Supplier<String> tokenSupplier;
2726

2827
public HttpBearerAuth(String scheme) {
29-
this.scheme = scheme;
28+
this.scheme = upperCaseBearer(scheme);
3029
}
3130

3231
/**
@@ -58,15 +57,14 @@ public void setBearerToken(Supplier<String> tokenSupplier) {
5857

5958
@Override
6059
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams) {
61-
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
60+
String bearerToken = tokenSupplier != null ? tokenSupplier.get() : null;
6261
if (bearerToken == null) {
6362
return;
6463
}
65-
66-
headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken);
64+
headerParams.put("Authorization", (scheme != null ? scheme + " " : "") + bearerToken);
6765
}
6866

6967
private static String upperCaseBearer(String scheme) {
70-
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme;
68+
return "bearer".equalsIgnoreCase(scheme) ? "Bearer" : scheme;
7169
}
7270
}

0 commit comments

Comments
 (0)