Skip to content

Commit 8afb067

Browse files
authored
[go] Improve examples generation (#7576)
* fix go examples * use constructor with required args
1 parent e58b1f6 commit 8afb067

File tree

13 files changed

+38
-41
lines changed

13 files changed

+38
-41
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
464464
if (codegenParameter.isListContainer) { // array
465465
return codegenParameter.dataType + "{" + constructExampleCode(codegenParameter.items, modelMaps, processedModelMap) + "}";
466466
} else if (codegenParameter.isMap) {
467-
return "map[string]string{ \"Key\" = \"Value\" }";
467+
return codegenParameter.dataType + "{ \"key\": " + constructExampleCode(codegenParameter.items, modelMaps, processedModelMap) + "}";
468468
} else if (codegenParameter.isPrimitiveType) { // primitive type
469469
if (codegenParameter.isString) {
470470
if (StringUtils.isEmpty(codegenParameter.example)) {
@@ -479,9 +479,9 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
479479
return "false";
480480
}
481481
} else if (codegenParameter.isUri) { // URL
482-
return "URL(string: \"https://example.com\")!";
482+
return "\"https://example.com\"";
483483
} else if (codegenParameter.isDateTime || codegenParameter.isDate) { // datetime or date
484-
return "Get-Date";
484+
return "time.Now()";
485485
} else { // numeric
486486
if (StringUtils.isEmpty(codegenParameter.example)) {
487487
return codegenParameter.example;
@@ -502,9 +502,9 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
502502

503503
private String constructExampleCode(CodegenProperty codegenProperty, HashMap<String, CodegenModel> modelMaps, HashMap<String, Integer> processedModelMap) {
504504
if (codegenProperty.isListContainer) { // array
505-
return codegenProperty.dataType + "{" + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + ")";
505+
return codegenProperty.dataType + "{" + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "}";
506506
} else if (codegenProperty.isMap) { // map
507-
return "map[string]string{ \"Key\" = \"Value\" }";
507+
return codegenProperty.dataType + "{ \"key\": " + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "}";
508508
} else if (codegenProperty.isPrimitiveType) { // primitive type
509509
if (codegenProperty.isString) {
510510
if (StringUtils.isEmpty(codegenProperty.example)) {
@@ -519,7 +519,7 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
519519
return "false";
520520
}
521521
} else if (codegenProperty.isUri) { // URL
522-
return "\"https://example.com\")!";
522+
return "\"https://example.com\"";
523523
} else if (codegenProperty.isDateTime || codegenProperty.isDate) { // datetime or date
524524
return "time.Now()";
525525
} else { // numeric
@@ -565,13 +565,10 @@ private String constructExampleCode(CodegenModel codegenModel, HashMap<String, C
565565
processedModelMap.put(model, 1);
566566
}
567567

568-
example = "" + goImportAlias + "." + codegenModel.name + "{";
569568
List<String> propertyExamples = new ArrayList<>();
570-
for (CodegenProperty codegenProperty : codegenModel.allVars) {
571-
propertyExamples.add(codegenProperty.name + ": " + constructExampleCode(codegenProperty, modelMaps, processedModelMap));
569+
for (CodegenProperty codegenProperty : codegenModel.requiredVars) {
570+
propertyExamples.add(constructExampleCode(codegenProperty, modelMaps, processedModelMap));
572571
}
573-
example += StringUtils.join(propertyExamples, ", ");
574-
example += "}";
575-
return example;
572+
return "*" + goImportAlias + ".New" + codegenModel.name + "(" + StringUtils.join(propertyExamples, ", ") + ")";
576573
}
577574
}

samples/client/petstore/go/go-petstore/docs/AnotherFakeApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func main() {
32-
body := openapiclient.Client{Client: "Client_example"} // Client | client model
32+
body := *openapiclient.NewClient() // Client | client model
3333

3434
configuration := openapiclient.NewConfiguration()
3535
api_client := openapiclient.NewAPIClient(configuration)

samples/client/petstore/go/go-petstore/docs/FakeApi.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import (
4242
)
4343

4444
func main() {
45-
xmlItem := openapiclient.XmlItem{AttributeString: "AttributeString_example", AttributeNumber: 123, AttributeInteger: 123, AttributeBoolean: true, WrappedArray: []int32{123), NameString: "NameString_example", NameNumber: 123, NameInteger: 123, NameBoolean: true, NameArray: []int32{123), NameWrappedArray: []int32{123), PrefixString: "PrefixString_example", PrefixNumber: 123, PrefixInteger: 123, PrefixBoolean: true, PrefixArray: []int32{123), PrefixWrappedArray: []int32{123), NamespaceString: "NamespaceString_example", NamespaceNumber: 123, NamespaceInteger: 123, NamespaceBoolean: true, NamespaceArray: []int32{123), NamespaceWrappedArray: []int32{123), PrefixNsString: "PrefixNsString_example", PrefixNsNumber: 123, PrefixNsInteger: 123, PrefixNsBoolean: true, PrefixNsArray: []int32{123), PrefixNsWrappedArray: []int32{123)} // XmlItem | XmlItem Body
45+
xmlItem := *openapiclient.NewXmlItem() // XmlItem | XmlItem Body
4646

4747
configuration := openapiclient.NewConfiguration()
4848
api_client := openapiclient.NewAPIClient(configuration)
@@ -172,7 +172,7 @@ import (
172172
)
173173

174174
func main() {
175-
body := openapiclient.OuterComposite{MyNumber: 123, MyString: "MyString_example", MyBoolean: false} // OuterComposite | Input composite as post body (optional)
175+
body := *openapiclient.NewOuterComposite() // OuterComposite | Input composite as post body (optional)
176176

177177
configuration := openapiclient.NewConfiguration()
178178
api_client := openapiclient.NewAPIClient(configuration)
@@ -370,7 +370,7 @@ import (
370370
)
371371

372372
func main() {
373-
body := openapiclient.FileSchemaTestClass{File: openapiclient.File{SourceURI: "SourceURI_example"}, Files: []File{openapiclient.File{SourceURI: "SourceURI_example"})} // FileSchemaTestClass |
373+
body := *openapiclient.NewFileSchemaTestClass() // FileSchemaTestClass |
374374

375375
configuration := openapiclient.NewConfiguration()
376376
api_client := openapiclient.NewAPIClient(configuration)
@@ -433,7 +433,7 @@ import (
433433

434434
func main() {
435435
query := "query_example" // string |
436-
body := openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123} // User |
436+
body := *openapiclient.NewUser() // User |
437437

438438
configuration := openapiclient.NewConfiguration()
439439
api_client := openapiclient.NewAPIClient(configuration)
@@ -498,7 +498,7 @@ import (
498498
)
499499

500500
func main() {
501-
body := openapiclient.Client{Client: "Client_example"} // Client | client model
501+
body := *openapiclient.NewClient() // Client | client model
502502

503503
configuration := openapiclient.NewConfiguration()
504504
api_client := openapiclient.NewAPIClient(configuration)
@@ -574,8 +574,8 @@ func main() {
574574
float := 987 // float32 | None (optional)
575575
string_ := "string__example" // string | None (optional)
576576
binary := 987 // *os.File | None (optional)
577-
date := Get-Date // string | None (optional)
578-
dateTime := Get-Date // time.Time | None (optional)
577+
date := time.Now() // string | None (optional)
578+
dateTime := time.Now() // time.Time | None (optional)
579579
password := "password_example" // string | None (optional)
580580
callback := "callback_example" // string | None (optional)
581581

@@ -804,7 +804,7 @@ import (
804804
)
805805

806806
func main() {
807-
param := map[string]string{ "Key" = "Value" } // map[string]string | request body
807+
param := map[string]string{ "key": "Inner_example"} // map[string]string | request body
808808

809809
configuration := openapiclient.NewConfiguration()
810810
api_client := openapiclient.NewAPIClient(configuration)

samples/client/petstore/go/go-petstore/docs/FakeClassnameTags123Api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func main() {
32-
body := openapiclient.Client{Client: "Client_example"} // Client | client model
32+
body := *openapiclient.NewClient() // Client | client model
3333

3434
configuration := openapiclient.NewConfiguration()
3535
api_client := openapiclient.NewAPIClient(configuration)

samples/client/petstore/go/go-petstore/docs/PetApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
)
3636

3737
func main() {
38-
body := openapiclient.Pet{Id: int64(123), Category: openapiclient.Category{Id: int64(123), Name: "Name_example"}, Name: "Name_example", PhotoUrls: []string{"PhotoUrls_example"), Tags: []Tag{openapiclient.Tag{Id: int64(123), Name: "Name_example"}), Status: "Status_example"} // Pet | Pet object that needs to be added to the store
38+
body := *openapiclient.NewPet("Name_example", []string{"PhotoUrls_example"}) // Pet | Pet object that needs to be added to the store
3939

4040
configuration := openapiclient.NewConfiguration()
4141
api_client := openapiclient.NewAPIClient(configuration)
@@ -367,7 +367,7 @@ import (
367367
)
368368

369369
func main() {
370-
body := openapiclient.Pet{Id: int64(123), Category: openapiclient.Category{Id: int64(123), Name: "Name_example"}, Name: "Name_example", PhotoUrls: []string{"PhotoUrls_example"), Tags: []Tag{openapiclient.Tag{Id: int64(123), Name: "Name_example"}), Status: "Status_example"} // Pet | Pet object that needs to be added to the store
370+
body := *openapiclient.NewPet("Name_example", []string{"PhotoUrls_example"}) // Pet | Pet object that needs to be added to the store
371371

372372
configuration := openapiclient.NewConfiguration()
373373
api_client := openapiclient.NewAPIClient(configuration)

samples/client/petstore/go/go-petstore/docs/StoreApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ import (
229229
)
230230

231231
func main() {
232-
body := openapiclient.Order{Id: int64(123), PetId: int64(123), Quantity: 123, ShipDate: "TODO", Status: "Status_example", Complete: false} // Order | order placed for purchasing the pet
232+
body := *openapiclient.NewOrder() // Order | order placed for purchasing the pet
233233

234234
configuration := openapiclient.NewConfiguration()
235235
api_client := openapiclient.NewAPIClient(configuration)

samples/client/petstore/go/go-petstore/docs/UserApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
)
3737

3838
func main() {
39-
body := openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123} // User | Created user object
39+
body := *openapiclient.NewUser() // User | Created user object
4040

4141
configuration := openapiclient.NewConfiguration()
4242
api_client := openapiclient.NewAPIClient(configuration)
@@ -98,7 +98,7 @@ import (
9898
)
9999

100100
func main() {
101-
body := []User{openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123}} // []User | List of user object
101+
body := []User{*openapiclient.NewUser()} // []User | List of user object
102102

103103
configuration := openapiclient.NewConfiguration()
104104
api_client := openapiclient.NewAPIClient(configuration)

samples/openapi3/client/petstore/go/go-petstore/docs/AnotherFakeApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func main() {
32-
client := openapiclient.Client{Client: "Client_example"} // Client | client model
32+
client := *openapiclient.NewClient() // Client | client model
3333

3434
configuration := openapiclient.NewConfiguration()
3535
api_client := openapiclient.NewAPIClient(configuration)

samples/openapi3/client/petstore/go/go-petstore/docs/FakeApi.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ import (
167167
)
168168

169169
func main() {
170-
outerComposite := openapiclient.OuterComposite{MyNumber: 123, MyString: "MyString_example", MyBoolean: false} // OuterComposite | Input composite as post body (optional)
170+
outerComposite := *openapiclient.NewOuterComposite() // OuterComposite | Input composite as post body (optional)
171171

172172
configuration := openapiclient.NewConfiguration()
173173
api_client := openapiclient.NewAPIClient(configuration)
@@ -365,7 +365,7 @@ import (
365365
)
366366

367367
func main() {
368-
fileSchemaTestClass := openapiclient.FileSchemaTestClass{File: openapiclient.File{SourceURI: "SourceURI_example"}, Files: []File{openapiclient.File{SourceURI: "SourceURI_example"})} // FileSchemaTestClass |
368+
fileSchemaTestClass := *openapiclient.NewFileSchemaTestClass() // FileSchemaTestClass |
369369

370370
configuration := openapiclient.NewConfiguration()
371371
api_client := openapiclient.NewAPIClient(configuration)
@@ -428,7 +428,7 @@ import (
428428

429429
func main() {
430430
query := "query_example" // string |
431-
user := openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123, ArbitraryObject: 123, ArbitraryNullableObject: 123, ArbitraryTypeValue: 123, ArbitraryNullableTypeValue: 123} // User |
431+
user := *openapiclient.NewUser() // User |
432432

433433
configuration := openapiclient.NewConfiguration()
434434
api_client := openapiclient.NewAPIClient(configuration)
@@ -493,7 +493,7 @@ import (
493493
)
494494

495495
func main() {
496-
client := openapiclient.Client{Client: "Client_example"} // Client | client model
496+
client := *openapiclient.NewClient() // Client | client model
497497

498498
configuration := openapiclient.NewConfiguration()
499499
api_client := openapiclient.NewAPIClient(configuration)
@@ -569,8 +569,8 @@ func main() {
569569
float := 987 // float32 | None (optional)
570570
string_ := "string__example" // string | None (optional)
571571
binary := 987 // *os.File | None (optional)
572-
date := Get-Date // string | None (optional)
573-
dateTime := Get-Date // time.Time | None (optional)
572+
date := time.Now() // string | None (optional)
573+
dateTime := time.Now() // time.Time | None (optional)
574574
password := "password_example" // string | None (optional)
575575
callback := "callback_example" // string | None (optional)
576576

@@ -799,7 +799,7 @@ import (
799799
)
800800

801801
func main() {
802-
requestBody := map[string]string{ "Key" = "Value" } // map[string]string | request body
802+
requestBody := map[string]string{ "key": "Inner_example"} // map[string]string | request body
803803

804804
configuration := openapiclient.NewConfiguration()
805805
api_client := openapiclient.NewAPIClient(configuration)

samples/openapi3/client/petstore/go/go-petstore/docs/FakeClassnameTags123Api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func main() {
32-
client := openapiclient.Client{Client: "Client_example"} // Client | client model
32+
client := *openapiclient.NewClient() // Client | client model
3333

3434
configuration := openapiclient.NewConfiguration()
3535
api_client := openapiclient.NewAPIClient(configuration)

samples/openapi3/client/petstore/go/go-petstore/docs/PetApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
)
3636

3737
func main() {
38-
pet := openapiclient.Pet{Id: int64(123), Category: openapiclient.Category{Id: int64(123), Name: "Name_example"}, Name: "Name_example", PhotoUrls: []string{"PhotoUrls_example"), Tags: []Tag{openapiclient.Tag{Id: int64(123), Name: "Name_example"}), Status: "Status_example"} // Pet | Pet object that needs to be added to the store
38+
pet := *openapiclient.NewPet("Name_example", []string{"PhotoUrls_example"}) // Pet | Pet object that needs to be added to the store
3939

4040
configuration := openapiclient.NewConfiguration()
4141
api_client := openapiclient.NewAPIClient(configuration)
@@ -367,7 +367,7 @@ import (
367367
)
368368

369369
func main() {
370-
pet := openapiclient.Pet{Id: int64(123), Category: openapiclient.Category{Id: int64(123), Name: "Name_example"}, Name: "Name_example", PhotoUrls: []string{"PhotoUrls_example"), Tags: []Tag{openapiclient.Tag{Id: int64(123), Name: "Name_example"}), Status: "Status_example"} // Pet | Pet object that needs to be added to the store
370+
pet := *openapiclient.NewPet("Name_example", []string{"PhotoUrls_example"}) // Pet | Pet object that needs to be added to the store
371371

372372
configuration := openapiclient.NewConfiguration()
373373
api_client := openapiclient.NewAPIClient(configuration)

samples/openapi3/client/petstore/go/go-petstore/docs/StoreApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ import (
229229
)
230230

231231
func main() {
232-
order := openapiclient.Order{Id: int64(123), PetId: int64(123), Quantity: 123, ShipDate: "TODO", Status: "Status_example", Complete: false} // Order | order placed for purchasing the pet
232+
order := *openapiclient.NewOrder() // Order | order placed for purchasing the pet
233233

234234
configuration := openapiclient.NewConfiguration()
235235
api_client := openapiclient.NewAPIClient(configuration)

samples/openapi3/client/petstore/go/go-petstore/docs/UserApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
)
3737

3838
func main() {
39-
user := openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123, ArbitraryObject: 123, ArbitraryNullableObject: 123, ArbitraryTypeValue: 123, ArbitraryNullableTypeValue: 123} // User | Created user object
39+
user := *openapiclient.NewUser() // User | Created user object
4040

4141
configuration := openapiclient.NewConfiguration()
4242
api_client := openapiclient.NewAPIClient(configuration)
@@ -98,7 +98,7 @@ import (
9898
)
9999

100100
func main() {
101-
user := []User{openapiclient.User{Id: int64(123), Username: "Username_example", FirstName: "FirstName_example", LastName: "LastName_example", Email: "Email_example", Password: "Password_example", Phone: "Phone_example", UserStatus: 123, ArbitraryObject: 123, ArbitraryNullableObject: 123, ArbitraryTypeValue: 123, ArbitraryNullableTypeValue: 123}} // []User | List of user object
101+
user := []User{*openapiclient.NewUser()} // []User | List of user object
102102

103103
configuration := openapiclient.NewConfiguration()
104104
api_client := openapiclient.NewAPIClient(configuration)

0 commit comments

Comments
 (0)