Skip to content

Commit c78f033

Browse files
authored
chore: remove unnecessary builders for type aliases (#39)
1 parent 0a09a53 commit c78f033

32 files changed

Lines changed: 120 additions & 1001 deletions

codegen/internal/generator/additional_properties_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,82 @@ func TestGenerateModelWithPropertiesAndAdditionalProperties(t *testing.T) {
6565
assertContains(t, generated, "public Builder additionalProperty(String name, Object value)")
6666
}
6767

68+
func TestGenerateModelWithoutBuilderForTypeAliases(t *testing.T) {
69+
t.Parallel()
70+
71+
tmp := t.TempDir()
72+
specPath := filepath.Join(tmp, "openapi.json")
73+
outputDir := filepath.Join(tmp, "src", "main", "java")
74+
resourceDir := filepath.Join(tmp, "src", "main", "resources")
75+
76+
spec := `{
77+
"openapi": "3.0.3",
78+
"info": {
79+
"title": "test",
80+
"version": "1.0.0"
81+
},
82+
"paths": {},
83+
"components": {
84+
"schemas": {
85+
"Lon": {
86+
"type": "number",
87+
"format": "float"
88+
},
89+
"Meta": {
90+
"type": "object"
91+
}
92+
}
93+
}
94+
}`
95+
if err := os.WriteFile(specPath, []byte(spec), 0o644); err != nil {
96+
t.Fatalf("write spec: %v", err)
97+
}
98+
99+
params := Params{
100+
SpecPath: specPath,
101+
OutputDir: outputDir,
102+
ResourceDir: resourceDir,
103+
BasePackage: "com.test.sdk",
104+
}
105+
if err := Run(context.Background(), params); err != nil {
106+
t.Fatalf("run generator: %v", err)
107+
}
108+
109+
lonPath := filepath.Join(outputDir, "com", "test", "sdk", "models", "Lon.java")
110+
lonContent, err := os.ReadFile(lonPath)
111+
if err != nil {
112+
t.Fatalf("read generated Lon model: %v", err)
113+
}
114+
lonGenerated := string(lonContent)
115+
116+
assertContains(t, lonGenerated, "public record Lon(")
117+
assertContains(t, lonGenerated, "Float value")
118+
assertNotContains(t, lonGenerated, "public static Builder builder()")
119+
assertNotContains(t, lonGenerated, "public static final class Builder")
120+
121+
metaPath := filepath.Join(outputDir, "com", "test", "sdk", "models", "Meta.java")
122+
metaContent, err := os.ReadFile(metaPath)
123+
if err != nil {
124+
t.Fatalf("read generated Meta model: %v", err)
125+
}
126+
metaGenerated := string(metaContent)
127+
128+
assertContains(t, metaGenerated, "public record Meta(")
129+
assertContains(t, metaGenerated, "java.util.Map<String, Object> value")
130+
assertNotContains(t, metaGenerated, "public static Builder builder()")
131+
assertNotContains(t, metaGenerated, "public static final class Builder")
132+
}
133+
68134
func assertContains(t *testing.T, content, want string) {
69135
t.Helper()
70136
if !strings.Contains(content, want) {
71137
t.Fatalf("expected generated output to contain %q", want)
72138
}
73139
}
140+
141+
func assertNotContains(t *testing.T, content, want string) {
142+
t.Helper()
143+
if strings.Contains(content, want) {
144+
t.Fatalf("expected generated output to not contain %q", want)
145+
}
146+
}

codegen/internal/generator/model.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type schemaModel struct {
8989
AdditionalProps *additionalPropertiesModel
9090
Imports []string
9191
HasRequired bool
92+
HasBuilder bool
9293
IsEnum bool
9394
EnumValues []enumValueModel
9495
}
@@ -547,7 +548,8 @@ func buildSchemas(doc *v3.Document, params Params, resolver *typeResolver) []sch
547548
if additionalProps != nil {
548549
imports = withAdditionalPropertiesImports(imports)
549550
}
550-
if hasRequired {
551+
hasBuilder := shouldGenerateBuilder(fields, additionalProps)
552+
if hasRequired && hasBuilder {
551553
imports = uniqueStrings(append(imports, "java.util.Objects"))
552554
}
553555
model := schemaModel{
@@ -559,12 +561,23 @@ func buildSchemas(doc *v3.Document, params Params, resolver *typeResolver) []sch
559561
AdditionalProps: additionalProps,
560562
Imports: imports,
561563
HasRequired: hasRequired,
564+
HasBuilder: hasBuilder,
562565
}
563566
result = append(result, model)
564567
}
565568
return result
566569
}
567570

571+
// shouldGenerateBuilder reports whether the model should expose a builder.
572+
// Single-field wrapper records (for example Lon/Lat/Meta-style aliases) don't
573+
// benefit from a builder and should use the canonical record constructor.
574+
func shouldGenerateBuilder(fields []schemaField, additionalProps *additionalPropertiesModel) bool {
575+
if additionalProps != nil {
576+
return true
577+
}
578+
return !(len(fields) == 1 && fields[0].Name == "value")
579+
}
580+
568581
// buildSchemaFields inspects a schema proxy and returns the fields, required
569582
// imports, and a flag indicating whether any required properties exist.
570583
func buildSchemaFields(name string, ref *base.SchemaProxy, resolver *typeResolver) ([]schemaField, *additionalPropertiesModel, []string, bool) {

codegen/internal/generator/render.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ func prepareModelTemplateData(schema schemaModel) map[string]any {
371371
"Fields": schema.Fields,
372372
"AdditionalProps": schema.AdditionalProps,
373373
"HasRequired": schema.HasRequired,
374+
"HasBuilder": schema.HasBuilder,
374375
"IsEnum": schema.IsEnum,
375376
"EnumValues": schema.EnumValues,
376377
}

codegen/internal/generator/templates/model.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public record {{ .ClassName }}(
7878
}
7979

8080
{{- end }}
81+
{{- if .HasBuilder }}
8182
/**
8283
* Creates a builder for {{ .ClassName }}.
8384
*
@@ -164,5 +165,6 @@ public record {{ .ClassName }}(
164165
{{- end }}
165166
}
166167
}
168+
{{- end }}
167169
}
168170
{{- end }}

codegen/internal/generator/types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ func (r *typeResolver) inlineSchemaModels(params Params) []schemaModel {
349349
if additionalProps != nil {
350350
imports = withAdditionalPropertiesImports(imports)
351351
}
352-
if hasRequired {
352+
hasBuilder := shouldGenerateBuilder(fields, additionalProps)
353+
if hasRequired && hasBuilder {
353354
imports = uniqueStrings(append(imports, "java.util.Objects"))
354355
}
355356
models = append(models, schemaModel{
@@ -361,6 +362,7 @@ func (r *typeResolver) inlineSchemaModels(params Params) []schemaModel {
361362
AdditionalProps: additionalProps,
362363
Imports: imports,
363364
HasRequired: hasRequired,
365+
HasBuilder: hasBuilder,
364366
})
365367
info.processed = true
366368
added = true

src/main/java/com/sumup/sdk/models/Attributes.java

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,4 @@
22
package com.sumup.sdk.models;
33

44
/** Object attributes that are modifiable only by SumUp applications. */
5-
public record Attributes(java.util.Map<String, Object> value) {
6-
/**
7-
* Creates a builder for Attributes.
8-
*
9-
* @return Builder that constructs immutable Attributes instances.
10-
*/
11-
public static Builder builder() {
12-
return new Builder();
13-
}
14-
15-
/** Builder for Attributes instances. */
16-
public static final class Builder {
17-
private java.util.Map<String, Object> value;
18-
19-
private Builder() {}
20-
21-
/**
22-
* Sets the value for {@code value}.
23-
*
24-
* @param value Value for the value field.
25-
* @return This builder instance.
26-
*/
27-
public Builder value(java.util.Map<String, Object> value) {
28-
this.value = value;
29-
return this;
30-
}
31-
32-
/**
33-
* Builds an immutable Attributes instance.
34-
*
35-
* @return Immutable Attributes.
36-
*/
37-
public Attributes build() {
38-
return new Attributes(value);
39-
}
40-
}
41-
}
5+
public record Attributes(java.util.Map<String, Object> value) {}

src/main/java/com/sumup/sdk/models/BusinessOwners.java

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,4 @@
22
package com.sumup.sdk.models;
33

44
/** Business owners information. */
5-
public record BusinessOwners(java.util.List<com.sumup.sdk.models.BusinessOwnersItem> value) {
6-
/**
7-
* Creates a builder for BusinessOwners.
8-
*
9-
* @return Builder that constructs immutable BusinessOwners instances.
10-
*/
11-
public static Builder builder() {
12-
return new Builder();
13-
}
14-
15-
/** Builder for BusinessOwners instances. */
16-
public static final class Builder {
17-
private java.util.List<com.sumup.sdk.models.BusinessOwnersItem> value;
18-
19-
private Builder() {}
20-
21-
/**
22-
* Sets the value for {@code value}.
23-
*
24-
* @param value Value for the value field.
25-
* @return This builder instance.
26-
*/
27-
public Builder value(java.util.List<com.sumup.sdk.models.BusinessOwnersItem> value) {
28-
this.value = value;
29-
return this;
30-
}
31-
32-
/**
33-
* Builds an immutable BusinessOwners instance.
34-
*
35-
* @return Immutable BusinessOwners.
36-
*/
37-
public BusinessOwners build() {
38-
return new BusinessOwners(value);
39-
}
40-
}
41-
}
5+
public record BusinessOwners(java.util.List<com.sumup.sdk.models.BusinessOwnersItem> value) {}

src/main/java/com/sumup/sdk/models/ChangeStatus.java

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,4 @@
77
* have been applied, the status `done`. The status is only returned after write operations or on
88
* read endpoints when the `version` query parameter is provided.
99
*/
10-
public record ChangeStatus(String value) {
11-
/**
12-
* Creates a builder for ChangeStatus.
13-
*
14-
* @return Builder that constructs immutable ChangeStatus instances.
15-
*/
16-
public static Builder builder() {
17-
return new Builder();
18-
}
19-
20-
/** Builder for ChangeStatus instances. */
21-
public static final class Builder {
22-
private String value;
23-
24-
private Builder() {}
25-
26-
/**
27-
* Sets the value for {@code value}.
28-
*
29-
* @param value Value for the value field.
30-
* @return This builder instance.
31-
*/
32-
public Builder value(String value) {
33-
this.value = value;
34-
return this;
35-
}
36-
37-
/**
38-
* Builds an immutable ChangeStatus instance.
39-
*
40-
* @return Immutable ChangeStatus.
41-
*/
42-
public ChangeStatus build() {
43-
return new ChangeStatus(value);
44-
}
45-
}
46-
}
10+
public record ChangeStatus(String value) {}
Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,4 @@
11
// Code generated by sumup-java/codegen. DO NOT EDIT.
22
package com.sumup.sdk.models;
33

4-
public record CheckoutSuccess(com.sumup.sdk.models.Checkout value) {
5-
/**
6-
* Creates a builder for CheckoutSuccess.
7-
*
8-
* @return Builder that constructs immutable CheckoutSuccess instances.
9-
*/
10-
public static Builder builder() {
11-
return new Builder();
12-
}
13-
14-
/** Builder for CheckoutSuccess instances. */
15-
public static final class Builder {
16-
private com.sumup.sdk.models.Checkout value;
17-
18-
private Builder() {}
19-
20-
/**
21-
* Sets the value for {@code value}.
22-
*
23-
* @param value Value for the value field.
24-
* @return This builder instance.
25-
*/
26-
public Builder value(com.sumup.sdk.models.Checkout value) {
27-
this.value = value;
28-
return this;
29-
}
30-
31-
/**
32-
* Builds an immutable CheckoutSuccess instance.
33-
*
34-
* @return Immutable CheckoutSuccess.
35-
*/
36-
public CheckoutSuccess build() {
37-
return new CheckoutSuccess(value);
38-
}
39-
}
40-
}
4+
public record CheckoutSuccess(com.sumup.sdk.models.Checkout value) {}

src/main/java/com/sumup/sdk/models/CompanyIdentifiers.java

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,4 @@
22
package com.sumup.sdk.models;
33

44
/** A list of country-specific company identifiers. */
5-
public record CompanyIdentifiers(java.util.List<com.sumup.sdk.models.CompanyIdentifier> value) {
6-
/**
7-
* Creates a builder for CompanyIdentifiers.
8-
*
9-
* @return Builder that constructs immutable CompanyIdentifiers instances.
10-
*/
11-
public static Builder builder() {
12-
return new Builder();
13-
}
14-
15-
/** Builder for CompanyIdentifiers instances. */
16-
public static final class Builder {
17-
private java.util.List<com.sumup.sdk.models.CompanyIdentifier> value;
18-
19-
private Builder() {}
20-
21-
/**
22-
* Sets the value for {@code value}.
23-
*
24-
* @param value Value for the value field.
25-
* @return This builder instance.
26-
*/
27-
public Builder value(java.util.List<com.sumup.sdk.models.CompanyIdentifier> value) {
28-
this.value = value;
29-
return this;
30-
}
31-
32-
/**
33-
* Builds an immutable CompanyIdentifiers instance.
34-
*
35-
* @return Immutable CompanyIdentifiers.
36-
*/
37-
public CompanyIdentifiers build() {
38-
return new CompanyIdentifiers(value);
39-
}
40-
}
41-
}
5+
public record CompanyIdentifiers(java.util.List<com.sumup.sdk.models.CompanyIdentifier> value) {}

0 commit comments

Comments
 (0)