Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -448,26 +448,44 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
for (CodegenParameter p : op.allParams) {
p.vendorExtensions.put("x-go-example", constructExampleCode(p, modelMaps, processedModelMaps));
}
processedModelMaps.clear();
}

processedModelMaps.clear();
for (CodegenOperation operation : operationList) {
boolean needTimeImport = false;
for (CodegenParameter cp : operation.allParams) {
cp.vendorExtensions.put("x-go-example", constructExampleCode(cp, modelMaps, processedModelMaps));
if (cp.isDateTime || cp.isDate) { // datetime or date
needTimeImport = true;
}
}
if (needTimeImport) {
operation.vendorExtensions.put("x-go-import", " \"time\"");
}
processedModelMaps.clear();
}

return objs;
}

private String constructExampleCode(CodegenParameter codegenParameter, HashMap<String, CodegenModel> modelMaps, HashMap<String, Integer> processedModelMap) {
if (codegenParameter.isArray) { // array
return codegenParameter.dataType + "{" + constructExampleCode(codegenParameter.items, modelMaps, processedModelMap) + "}";
String prefix = codegenParameter.dataType;
String dataType = StringUtils.removeStart(codegenParameter.dataType, "[]");
if (modelMaps.containsKey(dataType)) {
prefix = "[]" + goImportAlias + "." + dataType;
}
return prefix + "{" + constructExampleCode(codegenParameter.items, modelMaps, processedModelMap) + "}";
} else if (codegenParameter.isMap) {
return codegenParameter.dataType + "{ \"key\": " + constructExampleCode(codegenParameter.items, modelMaps, processedModelMap) + "}";
String prefix = codegenParameter.dataType;
String dataType = StringUtils.removeStart(codegenParameter.dataType, "map[string][]");
if (modelMaps.containsKey(dataType)) {
prefix = "map[string][]" + goImportAlias + "." + dataType;
}
return prefix + "{\"key\": " + constructExampleCode(codegenParameter.items, modelMaps, processedModelMap) + "}";
} else if (codegenParameter.isPrimitiveType) { // primitive type
if (codegenParameter.isString) {
if (StringUtils.isEmpty(codegenParameter.example)) {
if (!StringUtils.isEmpty(codegenParameter.example) && codegenParameter.example != "null") {
return "\"" + codegenParameter.example + "\"";
} else {
return "\"" + codegenParameter.paramName + "_example\"";
Expand All @@ -482,11 +500,13 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
return "\"https://example.com\"";
} else if (codegenParameter.isDateTime || codegenParameter.isDate) { // datetime or date
return "time.Now()";
} else if (codegenParameter.isFile) {
return "os.NewFile(1234, \"some_file\")";
} else { // numeric
if (StringUtils.isEmpty(codegenParameter.example)) {
return codegenParameter.example;
if (!StringUtils.isEmpty(codegenParameter.example) && codegenParameter.example != "null") {
return codegenParameter.dataType + "(" + codegenParameter.example + ")";
} else {
return "987";
return codegenParameter.dataType + "(987)";
}
}
} else { // model
Expand All @@ -502,12 +522,22 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S

private String constructExampleCode(CodegenProperty codegenProperty, HashMap<String, CodegenModel> modelMaps, HashMap<String, Integer> processedModelMap) {
if (codegenProperty.isArray) { // array
return codegenProperty.dataType + "{" + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + ")";
String prefix = codegenProperty.dataType;
String dataType = StringUtils.removeStart(codegenProperty.dataType, "[]");
if (modelMaps.containsKey(dataType)) {
prefix = "[]" + goImportAlias + "." + dataType;
}
return prefix + "{" + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "}";
} else if (codegenProperty.isMap) { // map
return codegenProperty.dataType + "{ \"key\": " + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "}";
String prefix = codegenProperty.dataType;
String dataType = StringUtils.removeStart(codegenProperty.dataType, "map[string][]");
if (modelMaps.containsKey(dataType)) {
prefix = "map[string][]" + goImportAlias + "." + dataType;
}
return prefix + "{\"key\": " + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "}";
} else if (codegenProperty.isPrimitiveType) { // primitive type
if (codegenProperty.isString) {
if (StringUtils.isEmpty(codegenProperty.example)) {
if (!StringUtils.isEmpty(codegenProperty.example) && codegenProperty.example != "null") {
return "\"" + codegenProperty.example + "\"";
} else {
return "\"" + codegenProperty.name + "_example\"";
Expand All @@ -524,17 +554,13 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
return "time.Now()";
} else { // numeric
String example;
if (StringUtils.isEmpty(codegenProperty.example)) {
if (!StringUtils.isEmpty(codegenProperty.example) && codegenProperty.example != "null") {
example = codegenProperty.example;
} else {
example = "123";
}

if (codegenProperty.isLong) {
return "int64(" + example + ")";
} else {
return example;
}
return codegenProperty.dataType + "(" + example + ")";
}
} else {
// look up the model
Expand All @@ -548,8 +574,6 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
}

private String constructExampleCode(CodegenModel codegenModel, HashMap<String, CodegenModel> modelMaps, HashMap<String, Integer> processedModelMap) {
String example;

// break infinite recursion. Return, in case a model is already processed in the current context.
String model = codegenModel.name;
if (processedModelMap.containsKey(model)) {
Expand All @@ -561,6 +585,14 @@ private String constructExampleCode(CodegenModel codegenModel, HashMap<String, C
} else {
throw new RuntimeException("Invalid count when constructing example: " + count);
}
} else if (codegenModel.isEnum) {
Map<String, Object> allowableValues = codegenModel.allowableValues;
List<Object> values = (List<Object>) allowableValues.get("values");
return goImportAlias + "." + model + "(\"" + String.valueOf(values.get(0)) + "\")";
} else if (codegenModel.oneOf != null && !codegenModel.oneOf.isEmpty()) {
String subModel = (String) codegenModel.oneOf.toArray()[0];
String oneOf = constructExampleCode(modelMaps.get(subModel), modelMaps, processedModelMap).substring(1);
return goImportAlias + "." + model + "{" + subModel + ": " + oneOf + "}";
} else {
processedModelMap.put(model, 1);
}
Expand All @@ -569,6 +601,6 @@ private String constructExampleCode(CodegenModel codegenModel, HashMap<String, C
for (CodegenProperty codegenProperty : codegenModel.requiredVars) {
propertyExamples.add(constructExampleCode(codegenProperty, modelMaps, processedModelMap));
}
return "*" + goImportAlias + ".New" + codegenModel.name + "(" + StringUtils.join(propertyExamples, ", ") + ")";
return "*" + goImportAlias + ".New" + model + "(" + StringUtils.join(propertyExamples, ", ") + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (
"context"
"fmt"
"os"
{{#vendorExtensions.x-go-import}}
{{{vendorExtensions.x-go-import}}}
{{/vendorExtensions.x-go-import}}
{{goImportAlias}} "./openapi"
)

Expand Down Expand Up @@ -66,7 +69,7 @@ Other parameters are passed through a pointer to a api{{{nickname}}}Request stru

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------{{/-last}}{{/allParams}}{{#allParams}}
{{^isPathParam}} **{{paramName}}** | {{^isPrimitiveType}}{{^isFile}}[{{/isFile}}{{/isPrimitiveType}}**{{dataType}}**{{^isPrimitiveType}}{{^isFile}}]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}} | {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/isPathParam}}{{/allParams}}
{{^isPathParam}} **{{paramName}}** | {{#isContainer}}{{#isArray}}{{#items}}{{^isPrimitiveType}}{{^isFile}}[{{/isFile}}{{/isPrimitiveType}}**[]{{dataType}}**{{^isPrimitiveType}}{{^isFile}}]({{^baseType}}{{dataType}}{{/baseType}}{{#baseType}}{{baseType}}{{/baseType}}.md){{/isFile}}{{/isPrimitiveType}}{{/items}}{{/isArray}}{{#isMap}}{{#items}}{{^isPrimitiveType}}{{^isFile}}[{{/isFile}}{{/isPrimitiveType}}**map[string]{{dataType}}**{{^isPrimitiveType}}{{^isFile}}]({{^baseType}}{{dataType}}{{/baseType}}{{#baseType}}{{baseType}}{{/baseType}}.md){{/isFile}}{{/isPrimitiveType}}{{/items}}{{/isMap}}{{/isContainer}}{{^isContainer}}{{^isPrimitiveType}}{{^isFile}}[{{/isFile}}{{/isPrimitiveType}}**{{dataType}}**{{^isPrimitiveType}}{{^isFile}}]({{^baseType}}{{dataType}}{{/baseType}}{{#baseType}}{{baseType}}{{/baseType}}.md){{/isFile}}{{/isPrimitiveType}}{{/isContainer}} | {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/isPathParam}}{{/allParams}}

### Return type

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{#models}}{{#model}}# {{classname}}

{{^isEnum}}
## Properties

Name | Type | Description | Notes
Expand All @@ -12,7 +13,6 @@ Name | Type | Description | Notes
{{/vars}}
{{/vendorExtensions.x-is-one-of-interface}}

{{^isEnum}}
## Methods

{{^vendorExtensions.x-is-one-of-interface}}
Expand Down Expand Up @@ -84,6 +84,13 @@ Convenience method to wrap this instance of {{classname}} in {{{.}}}
{{/vendorExtensions.x-implements}}
{{/vendorExtensions.x-is-one-of-interface}}
{{/isEnum}}
{{#isEnum}}
## Enum

{{#allowableValues}}{{#enumVars}}
* `{{name}}` (value: `{{{value}}}`)
{{/enumVars}}{{/allowableValues}}
{{/isEnum}}

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
10 changes: 7 additions & 3 deletions samples/client/petstore/go/go-petstore/docs/EnumClass.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# EnumClass

## Properties
## Enum

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------

* `ABC` (value: `"_abc"`)

* `EFG` (value: `"-efg"`)

* `XYZ` (value: `"(xyz)"`)


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
Expand Down
51 changes: 26 additions & 25 deletions samples/client/petstore/go/go-petstore/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ import (
)

func main() {
body := 987 // float32 | Input number as post body (optional)
body := float32(8.14) // float32 | Input number as post body (optional)

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -560,20 +560,21 @@ import (
"context"
"fmt"
"os"
"time"
openapiclient "./openapi"
)

func main() {
number := 987 // float32 | None
double := 987 // float64 | None
number := float32(8.14) // float32 | None
double := float64(1.2) // float64 | None
patternWithoutDelimiter := "patternWithoutDelimiter_example" // string | None
byte_ := 987 // string | None
integer := 987 // int32 | None (optional)
int32_ := 987 // int32 | None (optional)
int64_ := 987 // int64 | None (optional)
float := 987 // float32 | None (optional)
byte_ := string(BYTE_ARRAY_DATA_HERE) // string | None
integer := int32(56) // int32 | None (optional)
int32_ := int32(56) // int32 | None (optional)
int64_ := int64(789) // int64 | None (optional)
float := float32(3.4) // float32 | None (optional)
string_ := "string__example" // string | None (optional)
binary := 987 // *os.File | None (optional)
binary := os.NewFile(1234, "some_file") // *os.File | None (optional)
date := time.Now() // string | None (optional)
dateTime := time.Now() // time.Time | None (optional)
password := "password_example" // string | None (optional)
Expand Down Expand Up @@ -658,8 +659,8 @@ func main() {
enumHeaderString := "enumHeaderString_example" // string | Header parameter enum test (string) (optional) (default to "-efg")
enumQueryStringArray := []string{"EnumQueryStringArray_example"} // []string | Query parameter enum test (string array) (optional)
enumQueryString := "enumQueryString_example" // string | Query parameter enum test (string) (optional) (default to "-efg")
enumQueryInteger := 987 // int32 | Query parameter enum test (double) (optional)
enumQueryDouble := 987 // float64 | Query parameter enum test (double) (optional)
enumQueryInteger := int32(56) // int32 | Query parameter enum test (double) (optional)
enumQueryDouble := float64(1.2) // float64 | Query parameter enum test (double) (optional)
enumFormStringArray := []string{"Inner_example"} // []string | Form parameter enum test (string array) (optional) (default to "$")
enumFormString := "enumFormString_example" // string | Form parameter enum test (string) (optional) (default to "-efg")

Expand All @@ -684,13 +685,13 @@ Other parameters are passed through a pointer to a apiTestEnumParametersRequest

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**enumHeaderStringArray** | [**[]string**](string.md) | Header parameter enum test (string array) |
**enumHeaderStringArray** | **[]string** | Header parameter enum test (string array) |
**enumHeaderString** | **string** | Header parameter enum test (string) | [default to &quot;-efg&quot;]
**enumQueryStringArray** | [**[]string**](string.md) | Query parameter enum test (string array) |
**enumQueryStringArray** | **[]string** | Query parameter enum test (string array) |
**enumQueryString** | **string** | Query parameter enum test (string) | [default to &quot;-efg&quot;]
**enumQueryInteger** | **int32** | Query parameter enum test (double) |
**enumQueryDouble** | **float64** | Query parameter enum test (double) |
**enumFormStringArray** | [**[]string**](string.md) | Form parameter enum test (string array) | [default to &quot;$&quot;]
**enumFormStringArray** | **[]string** | Form parameter enum test (string array) | [default to &quot;$&quot;]
**enumFormString** | **string** | Form parameter enum test (string) | [default to &quot;-efg&quot;]

### Return type
Expand Down Expand Up @@ -732,12 +733,12 @@ import (
)

func main() {
requiredStringGroup := 987 // int32 | Required String in group parameters
requiredStringGroup := int32(56) // int32 | Required String in group parameters
requiredBooleanGroup := true // bool | Required Boolean in group parameters
requiredInt64Group := 987 // int64 | Required Integer in group parameters
stringGroup := 987 // int32 | String in group parameters (optional)
requiredInt64Group := int64(789) // int64 | Required Integer in group parameters
stringGroup := int32(56) // int32 | String in group parameters (optional)
booleanGroup := true // bool | Boolean in group parameters (optional)
int64Group := 987 // int64 | Integer in group parameters (optional)
int64Group := int64(789) // int64 | Integer in group parameters (optional)

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand Down Expand Up @@ -804,7 +805,7 @@ import (
)

func main() {
param := map[string]string{ "key": "Inner_example"} // map[string]string | request body
param := map[string]string{"key": "Inner_example"} // map[string]string | request body

configuration := openapiclient.NewConfiguration()
api_client := openapiclient.NewAPIClient(configuration)
Expand All @@ -827,7 +828,7 @@ Other parameters are passed through a pointer to a apiTestInlineAdditionalProper

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**param** | [**map[string]string**](string.md) | request body |
**param** | **map[string]string** | request body |

### Return type

Expand Down Expand Up @@ -959,11 +960,11 @@ Other parameters are passed through a pointer to a apiTestQueryParameterCollecti

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**pipe** | [**[]string**](string.md) | |
**ioutil** | [**[]string**](string.md) | |
**http** | [**[]string**](string.md) | |
**url** | [**[]string**](string.md) | |
**context** | [**[]string**](string.md) | |
**pipe** | **[]string** | |
**ioutil** | **[]string** | |
**http** | **[]string** | |
**url** | **[]string** | |
**context** | **[]string** | |

### Return type

Expand Down
10 changes: 7 additions & 3 deletions samples/client/petstore/go/go-petstore/docs/OuterEnum.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# OuterEnum

## Properties
## Enum

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------

* `PLACED` (value: `"placed"`)

* `APPROVED` (value: `"approved"`)

* `DELIVERED` (value: `"delivered"`)


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
Expand Down
Loading