[Golang][go-gin-server] Fixed issue 1122 with workaround (#1141)

[Golang][go-gin-server] Fix path parameter naming
This commit is contained in:
kemokemo
2018-10-01 16:25:14 +09:00
committed by William Cheng
parent cbc84254c3
commit 2040050e55
13 changed files with 98 additions and 23 deletions

View File

@@ -18,6 +18,7 @@ package org.openapitools.codegen.languages;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.slf4j.Logger;
@@ -25,6 +26,8 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class GoGinServerCodegen extends AbstractGoCodegen {
@@ -85,6 +88,20 @@ public class GoGinServerCodegen extends AbstractGoCodegen {
);
}
@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
objs = super.postProcessOperationsWithModels(objs, allModels);
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation op : operationList) {
if (op.path != null) {
op.path = op.path.replaceAll("\\{(.*?)\\}", ":$1");
}
}
return objs;
}
@Override
public void processOpts() {
super.processOpts();

View File

@@ -20,8 +20,8 @@ To see how to make this your own, look here:
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
{{/infoUrl}}
### Running the server
To run the server, follow these simple steps:
```
@@ -33,9 +33,14 @@ To run the server in a docker container
docker build --network=host -t {{{packageName}}} .
```
Once image is built use
Once the image is built, just run
```
docker run --rm -it {{{packageName}}}
```
### Known Issue
Endpoints sharing a common path may result in issues. For example, `/v2/pet/findByTags` and `/v2/pet/:petId` will result in an issue with the Gin framework. For more information about this known limitation, please refer to [gin-gonic/gin#388](https://github.com/gin-gonic/gin/issues/388) for more information.
A workaround is to manually update the path and handler. Please refer to [gin-gonic/gin/issues/205#issuecomment-296155497](https://github.com/gin-gonic/gin/issues/205#issuecomment-296155497) for more information.

View File

@@ -1 +1 @@
3.2.3-SNAPSHOT
3.3.0-SNAPSHOT

View File

@@ -1487,7 +1487,9 @@ components:
type: boolean
type: object
indirect_map:
$ref: '#/components/schemas/StringBooleanMap'
additionalProperties:
type: boolean
type: object
type: object
Tag:
example:

View File

@@ -39,7 +39,7 @@ Name | Type | Description | Notes
### Return type
[**bool**](boolean.md)
**bool**
### Authorization
@@ -109,7 +109,7 @@ Name | Type | Description | Notes
### Return type
[**float32**](number.md)
**float32**
### Authorization

View File

@@ -6,7 +6,7 @@ Name | Type | Description | Notes
**MapMapOfString** | [**map[string]map[string]string**](map.md) | | [optional]
**MapOfEnumString** | **map[string]string** | | [optional]
**DirectMap** | **map[string]bool** | | [optional]
**IndirectMap** | [**StringBooleanMap**](StringBooleanMap.md) | | [optional]
**IndirectMap** | **map[string]bool** | | [optional]
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@@ -14,5 +14,5 @@ type MapTest struct {
MapMapOfString map[string]map[string]string `json:"map_map_of_string,omitempty" xml:"map_map_of_string"`
MapOfEnumString map[string]string `json:"map_of_enum_string,omitempty" xml:"map_of_enum_string"`
DirectMap map[string]bool `json:"direct_map,omitempty" xml:"direct_map"`
IndirectMap StringBooleanMap `json:"indirect_map,omitempty" xml:"indirect_map"`
IndirectMap map[string]bool `json:"indirect_map,omitempty" xml:"indirect_map"`
}

View File

@@ -1487,7 +1487,9 @@ components:
type: boolean
type: object
indirect_map:
$ref: '#/components/schemas/StringBooleanMap'
additionalProperties:
type: boolean
type: object
type: object
Tag:
example:

View File

@@ -39,7 +39,7 @@ Name | Type | Description | Notes
### Return type
[**bool**](boolean.md)
**bool**
### Authorization
@@ -109,7 +109,7 @@ Name | Type | Description | Notes
### Return type
[**float32**](number.md)
**float32**
### Authorization

View File

@@ -6,7 +6,7 @@ Name | Type | Description | Notes
**MapMapOfString** | [**map[string]map[string]string**](map.md) | | [optional]
**MapOfEnumString** | **map[string]string** | | [optional]
**DirectMap** | **map[string]bool** | | [optional]
**IndirectMap** | [**StringBooleanMap**](StringBooleanMap.md) | | [optional]
**IndirectMap** | **map[string]bool** | | [optional]
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@@ -13,5 +13,5 @@ type MapTest struct {
MapMapOfString map[string]map[string]string `json:"map_map_of_string,omitempty"`
MapOfEnumString map[string]string `json:"map_of_enum_string,omitempty"`
DirectMap map[string]bool `json:"direct_map,omitempty"`
IndirectMap StringBooleanMap `json:"indirect_map,omitempty"`
IndirectMap map[string]bool `json:"indirect_map,omitempty"`
}

View File

@@ -10,7 +10,7 @@ By using the [OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) from a
To see how to make this your own, look here:
[README]((https://openapi-generator.tech))
[README](https://openapi-generator.tech)
- API version: 1.0.0
@@ -32,4 +32,53 @@ Once image is built use
docker run --rm -it petstoreserver
```
### Known Issue
The endpoint `/v2/pet/findByTags` and `/v2/pet/:petId` are conflict with gin.
This is a known issue of gin. Please refer [gin-gonic/gin#388](https://github.com/gin-gonic/gin/issues/388)
You can manually fix it by updating the path and handler. Please refer [gin-gonic/gin/issues/205#issuecomment-296155497](https://github.com/gin-gonic/gin/issues/205#issuecomment-296155497) and an example below.
`routers.go`
```diff
var routes = Routes{
{
"Index",
"GET",
"/v2/",
Index,
},
- {
- "FindPetsByTags",
- strings.ToUpper("Get"),
- "/v2/pet/findByTags",
- FindPetsByTags,
- },
{
"GetPetById",
strings.ToUpper("Get"),
"/v2/pet/:petId",
GetPetById,
},
}
```
`api_pet.go`
```diff
// GetPetById - Find pet by ID
func GetPetById(c *gin.Context) {
- c.JSON(http.StatusOK, gin.H{})
+ petId := c.Param("petId")
+
+ if petId == "findByTags" {
+ FindPetsByTags(c)
+ } else {
+ c.JSON(http.StatusOK, gin.H{})
+ }
}
```

View File

@@ -73,7 +73,7 @@ var routes = Routes{
{
"DeletePet",
strings.ToUpper("Delete"),
"/v2/pet/{petId}",
"/v2/pet/:petId",
DeletePet,
},
@@ -94,7 +94,7 @@ var routes = Routes{
{
"GetPetById",
strings.ToUpper("Get"),
"/v2/pet/{petId}",
"/v2/pet/:petId",
GetPetById,
},
@@ -108,21 +108,21 @@ var routes = Routes{
{
"UpdatePetWithForm",
strings.ToUpper("Post"),
"/v2/pet/{petId}",
"/v2/pet/:petId",
UpdatePetWithForm,
},
{
"UploadFile",
strings.ToUpper("Post"),
"/v2/pet/{petId}/uploadImage",
"/v2/pet/:petId/uploadImage",
UploadFile,
},
{
"DeleteOrder",
strings.ToUpper("Delete"),
"/v2/store/order/{orderId}",
"/v2/store/order/:orderId",
DeleteOrder,
},
@@ -136,7 +136,7 @@ var routes = Routes{
{
"GetOrderById",
strings.ToUpper("Get"),
"/v2/store/order/{orderId}",
"/v2/store/order/:orderId",
GetOrderById,
},
@@ -171,14 +171,14 @@ var routes = Routes{
{
"DeleteUser",
strings.ToUpper("Delete"),
"/v2/user/{username}",
"/v2/user/:username",
DeleteUser,
},
{
"GetUserByName",
strings.ToUpper("Get"),
"/v2/user/{username}",
"/v2/user/:username",
GetUserByName,
},
@@ -199,7 +199,7 @@ var routes = Routes{
{
"UpdateUser",
strings.ToUpper("Put"),
"/v2/user/{username}",
"/v2/user/:username",
UpdateUser,
},
}