Skip to content

Commit fbc576d

Browse files
committed
Simplify spacing
1 parent c940aef commit fbc576d

12 files changed

Lines changed: 82 additions & 65 deletions

File tree

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

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,12 @@ protected void applyJakartaPackage() {
858858
writePropertyBack(JAVAX_PACKAGE, "jakarta");
859859
}
860860

861+
/**
862+
* Configure the generator for jspecify.
863+
*
864+
* override Nullable import to use the jspecify version.
865+
* add package-info.java to the model and api packages.
866+
*/
861867
protected void applyJspecify() {
862868
importMapping.put("Nullable", "org.jspecify.annotations.Nullable");
863869
if (Boolean.TRUE.equals(additionalProperties.get(CodegenConstants.GENERATE_MODELS))) {
@@ -2682,28 +2688,39 @@ public void setEnumPropertyNaming(final String enumPropertyNamingType) {
26822688
@Override
26832689
protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
26842690
this.jSpecifyNullableLambda = new JSpecifyNullableLambda();
2685-
// Add jSpecify nullable annotation in the correct location before or inside a declartion
2691+
// Add jSpecify nullable annotation in the correct location before or inside a declaration
2692+
// use cases:
2693+
//
2694+
// private {{#lambda.jSpecifyDatatype}}{{{dataType}}}{{/lambda.jSpecifyDatatype}} {{param}}
2695+
// private @Nullable Time param
2696+
// private java.time.@Nullable Time
2697+
// private Time param
2698+
//
2699+
// {{#lambda.jSpecifyDatatype}}{{{dataType}}}{{/lambda.jSpecifyDatatype}} {{param}}
2700+
// @Nullable Time param
2701+
// java.time.@Nullable Time
2702+
// Time param
2703+
//
2704+
// {{#lambda.jSpecifyNullable}}@Nullable {{/lambda.jSpecifyNullable}}{{#lambda.jSpecifyDatatype}}{{{dataType}}}{{/lambda.jSpecifyDatatype}}
2705+
// @Nullable Time
2706+
// @java.time.@Nullable Time
26862707
Mustache.Lambda jSpecifyDatatypeLambda = (fragment, writer) -> {
26872708
String dataType = fragment.execute();
2688-
if (jSpecifyNullableLambda.keptNullable) {
2689-
jSpecifyNullableLambda.keptNullable = false;
2709+
if (jSpecifyNullableLambda.keptNullable != null ) {
26902710
int idx = dataType.lastIndexOf('.');
26912711
if (idx > 0) {
26922712
// generate declaration like java.time.@Nullable Timestamp
26932713
writer.write(dataType.substring(0, idx + 1));
26942714
writer.write("@Nullable ");
26952715
writer.write(dataType.substring(idx + 1));
26962716
} else {
2697-
if (dataType.startsWith(" ")) {
2698-
writer.write(" @Nullable");
2699-
writer.write(dataType);
2700-
} else {
2701-
writer.write("@Nullable ");
2702-
writer.write(dataType);
2703-
}
2717+
writer.write("@Nullable ");
2718+
writer.write(dataType);
27042719
}
2720+
jSpecifyNullableLambda.keptNullable = null;
27052721
} else {
27062722
writer.write(dataType);
2723+
// writer.write(" ");
27072724
}
27082725
};
27092726
return super.addMustacheLambdas()
@@ -2718,19 +2735,19 @@ protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
27182735
class JSpecifyNullableLambda implements Mustache.Lambda {
27192736
private String nullableAnnotation = "@Nullable";
27202737
// remember if @Nullable is needed
2721-
boolean keptNullable = false;
2738+
String keptNullable = null;
27222739

27232740
public void setNullableAnnotation(String nullableAnnotation) {
27242741
this.nullableAnnotation = nullableAnnotation;
27252742
}
27262743

27272744
@Override
27282745
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
2729-
keptNullable = false;
2746+
keptNullable = null;
27302747
String value = fragment.execute();
27312748
if (useJspecify) {
27322749
if (value.startsWith(nullableAnnotation)) {
2733-
keptNullable = true;
2750+
keptNullable = value;
27342751
int idx = nullableAnnotation.length();
27352752
// trim left
27362753
while (idx < value.length() && value.charAt(idx) == ' ') {

modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,11 +907,11 @@ public class {{classname}} {
907907
public static final class API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {
908908
{{#requiredParams}}
909909
{{>nullable_var_annotations}}{{! prevent indent}}
910-
private{{>nullableDataType}} {{paramName}}; // {{description}} (required)
910+
private {{>nullableDataType}} {{paramName}}; // {{description}} (required)
911911
{{/requiredParams}}
912912
{{#optionalParams}}
913913
{{>nullable_var_annotations}}{{! prevent indent}}
914-
private{{>nullableDataType}} {{paramName}}; // {{description}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}})
914+
private {{>nullableDataType}} {{paramName}}; // {{description}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}})
915915
{{/optionalParams}}
916916

917917
private API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request(Builder builder) {

modules/openapi-generator/src/main/resources/Java/libraries/native/pojo.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
7878
{{/vendorExtensions.x-is-jackson-optional-nullable}}
7979
{{^vendorExtensions.x-is-jackson-optional-nullable}}
8080
{{>nullable_var_annotations}}{{! prevent indent}}
81-
private{{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
81+
private {{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
8282
{{/vendorExtensions.x-is-jackson-optional-nullable}}
8383

8484
{{/vars}}
@@ -209,7 +209,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
209209
{{!unannotated, Jackson would pick this up automatically and add it *in addition* to the _JsonNullable getter field}}
210210
@JsonIgnore
211211
{{/vendorExtensions.x-is-jackson-optional-nullable}}
212-
{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#jackson}}{{> jackson_annotations}}{{/jackson}}{{/vendorExtensions.x-is-jackson-optional-nullable}} public{{>nullableDatatypeWithEnum}} {{getter}}() {
212+
{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#jackson}}{{> jackson_annotations}}{{/jackson}}{{/vendorExtensions.x-is-jackson-optional-nullable}} public {{>nullableDatatypeWithEnum}} {{getter}}() {
213213
{{#vendorExtensions.x-is-jackson-optional-nullable}}
214214
{{#isReadOnly}}{{! A readonly attribute doesn't have setter => jackson will set null directly if explicitly returned by API, so make sure we have an empty JsonNullable}}
215215
if ({{name}} == null) {

modules/openapi-generator/src/main/resources/Java/libraries/restclient/pojo.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ public {{>sealed}}class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#v
8686
{{/vendorExtensions.x-is-jackson-optional-nullable}}
8787
{{^vendorExtensions.x-is-jackson-optional-nullable}}
8888
{{#isContainer}}
89-
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}}{{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
89+
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
9090
{{/isContainer}}
9191
{{^isContainer}}
92-
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}}{{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
92+
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
9393
{{/isContainer}}
9494
{{/vendorExtensions.x-is-jackson-optional-nullable}}
9595

@@ -240,7 +240,7 @@ public {{>sealed}}class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#v
240240
@JsonIgnore
241241
{{/vendorExtensions.x-is-jackson-optional-nullable}}
242242
{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#jackson}}{{> jackson_annotations}}{{/jackson}}{{/vendorExtensions.x-is-jackson-optional-nullable}}
243-
public{{>nullableDatatypeWithEnum}} {{getter}}() {
243+
public {{>nullableDatatypeWithEnum}} {{getter}}() {
244244
{{#vendorExtensions.x-is-jackson-optional-nullable}}
245245
{{#isReadOnly}}{{! A readonly attribute doesn't have setter => jackson will set null directly if explicitly returned by API, so make sure we have an empty JsonNullable}}
246246
if ({{name}} == null) {

modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/pojo.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
8686
{{/vendorExtensions.x-is-jackson-optional-nullable}}
8787
{{^vendorExtensions.x-is-jackson-optional-nullable}}
8888
{{#isContainer}}
89-
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}}{{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
89+
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
9090
{{/isContainer}}
9191
{{^isContainer}}
92-
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}}{{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
92+
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
9393
{{/isContainer}}
9494
{{/vendorExtensions.x-is-jackson-optional-nullable}}
9595

@@ -240,7 +240,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
240240
@JsonIgnore
241241
{{/vendorExtensions.x-is-jackson-optional-nullable}}
242242
{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#jackson}}{{> jackson_annotations}}{{/jackson}}{{/vendorExtensions.x-is-jackson-optional-nullable}}
243-
public{{>nullableDatatypeWithEnum}} {{getter}}() {
243+
public {{>nullableDatatypeWithEnum}} {{getter}}() {
244244
{{#vendorExtensions.x-is-jackson-optional-nullable}}
245245
{{#isReadOnly}}{{! A readonly attribute doesn't have setter => jackson will set null directly if explicitly returned by API, so make sure we have an empty JsonNullable}}
246246
if ({{name}} == null) {

modules/openapi-generator/src/main/resources/Java/libraries/webclient/pojo.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ public {{>sealed}}class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#v
8686
{{/vendorExtensions.x-is-jackson-optional-nullable}}
8787
{{^vendorExtensions.x-is-jackson-optional-nullable}}
8888
{{#isContainer}}
89-
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}}{{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
89+
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
9090
{{/isContainer}}
9191
{{^isContainer}}
92-
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}}{{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
92+
{{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{>nullableDatatypeWithEnum}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
9393
{{/isContainer}}
9494
{{/vendorExtensions.x-is-jackson-optional-nullable}}
9595

@@ -240,7 +240,7 @@ public {{>sealed}}class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#v
240240
@JsonIgnore
241241
{{/vendorExtensions.x-is-jackson-optional-nullable}}
242242
{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#jackson}}{{> jackson_annotations}}{{/jackson}}{{/vendorExtensions.x-is-jackson-optional-nullable}}
243-
public{{>nullableDatatypeWithEnum}} {{getter}}() {
243+
public {{>nullableDatatypeWithEnum}} {{getter}}() {
244244
{{#vendorExtensions.x-is-jackson-optional-nullable}}
245245
{{#isReadOnly}}{{! A readonly attribute doesn't have setter => jackson will set null directly if explicitly returned by API, so make sure we have an empty JsonNullable}}
246246
if ({{name}} == null) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#lambda.jSpecifyDatatype}} {{{dataType}}}{{/lambda.jSpecifyDatatype}}
1+
{{#lambda.jSpecifyDatatype}}{{{dataType}}}{{/lambda.jSpecifyDatatype}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#lambda.jSpecifyDatatype}} {{{datatypeWithEnum}}}{{/lambda.jSpecifyDatatype}}
1+
{{#lambda.jSpecifyDatatype}}{{{datatypeWithEnum}}}{{/lambda.jSpecifyDatatype}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static void assertValidJavaSourceCode(String javaSourceCode) {
164164
config.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11);
165165
JavaParser parser = new JavaParser(config);
166166
ParseResult<CompilationUnit> parseResult = parser.parse(javaSourceCode);
167-
assertTrue(parseResult.isSuccessful(), String.valueOf(parseResult.getProblems()));
167+
assertTrue(parseResult.isSuccessful(), parseResult.getProblems() + "\n in " + javaSourceCode);
168168
}
169169

170170
public static void assertFileContains(Path path, String... lines) {

samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,17 +2099,17 @@ public CompletableFuture<Void> testGroupParameters(APITestGroupParametersRequest
20992099
*/
21002100
public CompletableFuture<Void> testGroupParameters(APITestGroupParametersRequest apiRequest, Map<String, String> headers) throws ApiException {
21012101
@javax.annotation.Nonnull
2102-
Integer requiredStringGroup = apiRequest.requiredStringGroup();
2102+
Integer requiredStringGroup = apiRequest.requiredStringGroup();
21032103
@javax.annotation.Nonnull
2104-
Boolean requiredBooleanGroup = apiRequest.requiredBooleanGroup();
2104+
Boolean requiredBooleanGroup = apiRequest.requiredBooleanGroup();
21052105
@javax.annotation.Nonnull
2106-
Long requiredInt64Group = apiRequest.requiredInt64Group();
2106+
Long requiredInt64Group = apiRequest.requiredInt64Group();
21072107
@javax.annotation.Nullable
2108-
Integer stringGroup = apiRequest.stringGroup();
2108+
Integer stringGroup = apiRequest.stringGroup();
21092109
@javax.annotation.Nullable
2110-
Boolean booleanGroup = apiRequest.booleanGroup();
2110+
Boolean booleanGroup = apiRequest.booleanGroup();
21112111
@javax.annotation.Nullable
2112-
Long int64Group = apiRequest.int64Group();
2112+
Long int64Group = apiRequest.int64Group();
21132113
return testGroupParameters(requiredStringGroup, requiredBooleanGroup, requiredInt64Group, stringGroup, booleanGroup, int64Group, headers);
21142114
}
21152115

@@ -2332,27 +2332,27 @@ private APITestGroupParametersRequest(Builder builder) {
23322332
this.int64Group = builder.int64Group;
23332333
}
23342334
@javax.annotation.Nonnull
2335-
public Integer requiredStringGroup() {
2335+
publicInteger requiredStringGroup() {
23362336
return requiredStringGroup;
23372337
}
23382338
@javax.annotation.Nonnull
2339-
public Boolean requiredBooleanGroup() {
2339+
publicBoolean requiredBooleanGroup() {
23402340
return requiredBooleanGroup;
23412341
}
23422342
@javax.annotation.Nonnull
2343-
public Long requiredInt64Group() {
2343+
publicLong requiredInt64Group() {
23442344
return requiredInt64Group;
23452345
}
23462346
@javax.annotation.Nullable
2347-
public Integer stringGroup() {
2347+
publicInteger stringGroup() {
23482348
return stringGroup;
23492349
}
23502350
@javax.annotation.Nullable
2351-
public Boolean booleanGroup() {
2351+
publicBoolean booleanGroup() {
23522352
return booleanGroup;
23532353
}
23542354
@javax.annotation.Nullable
2355-
public Long int64Group() {
2355+
publicLong int64Group() {
23562356
return int64Group;
23572357
}
23582358
public static Builder newBuilder() {

0 commit comments

Comments
 (0)