forked from loafle/openapi-generator-original
[GO Gin Server] Webhooks support: add missing webhook handlers (#17411)
* Implement postProcessWebhooksWithModels * Implement postProcessWebhooksWithModels * Add missing webhook handlers * Test webhook handler * Generate samples
This commit is contained in:
parent
b20c8db281
commit
a34eeaed77
@ -22,10 +22,7 @@ import io.swagger.v3.oas.models.media.Schema;
|
|||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.openapitools.codegen.*;
|
import org.openapitools.codegen.*;
|
||||||
import org.openapitools.codegen.model.ModelMap;
|
import org.openapitools.codegen.model.*;
|
||||||
import org.openapitools.codegen.model.ModelsMap;
|
|
||||||
import org.openapitools.codegen.model.OperationMap;
|
|
||||||
import org.openapitools.codegen.model.OperationsMap;
|
|
||||||
import org.openapitools.codegen.utils.ModelUtils;
|
import org.openapitools.codegen.utils.ModelUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -586,6 +583,105 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
|
|||||||
return objs;
|
return objs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WebhooksMap postProcessWebhooksWithModels(WebhooksMap objs, List<ModelMap> allModels) {
|
||||||
|
OperationMap objectMap = objs.getWebhooks();
|
||||||
|
List<CodegenOperation> operations = objectMap.getOperation();
|
||||||
|
|
||||||
|
for (CodegenOperation operation : operations) {
|
||||||
|
// http method verb conversion (e.g. PUT => Put)
|
||||||
|
operation.httpMethod = camelize(operation.httpMethod.toLowerCase(Locale.ROOT));
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove model imports to avoid error
|
||||||
|
List<Map<String, String>> imports = objs.getImports();
|
||||||
|
if (imports == null)
|
||||||
|
return objs;
|
||||||
|
|
||||||
|
Iterator<Map<String, String>> iterator = imports.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
String _import = iterator.next().get("import");
|
||||||
|
if (_import.startsWith(apiPackage()))
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
// this will only import "fmt" and "strings" if there are items in pathParams
|
||||||
|
for (CodegenOperation operation : operations) {
|
||||||
|
if (operation.pathParams != null && operation.pathParams.size() > 0) {
|
||||||
|
imports.add(createMapping("import", "strings"));
|
||||||
|
break; //just need to import once
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean addedTimeImport = false;
|
||||||
|
boolean addedOSImport = false;
|
||||||
|
boolean addedReflectImport = false;
|
||||||
|
for (CodegenOperation operation : operations) {
|
||||||
|
// import "os" if the operation uses files
|
||||||
|
if (!addedOSImport && "*os.File".equals(operation.returnType)) {
|
||||||
|
imports.add(createMapping("import", "os"));
|
||||||
|
addedOSImport = true;
|
||||||
|
}
|
||||||
|
for (CodegenParameter param : operation.allParams) {
|
||||||
|
// import "os" if the operation uses files
|
||||||
|
if (!addedOSImport && "*os.File".equals(param.dataType)) {
|
||||||
|
imports.add(createMapping("import", "os"));
|
||||||
|
addedOSImport = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// import "time" if the operation has a time parameter.
|
||||||
|
if (!addedTimeImport && "time.Time".equals(param.dataType)) {
|
||||||
|
imports.add(createMapping("import", "time"));
|
||||||
|
addedTimeImport = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// import "reflect" package if the parameter is collectionFormat=multi
|
||||||
|
if (!addedReflectImport && param.isCollectionFormatMulti) {
|
||||||
|
imports.add(createMapping("import", "reflect"));
|
||||||
|
addedReflectImport = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set x-exportParamName
|
||||||
|
char nameFirstChar = param.paramName.charAt(0);
|
||||||
|
if (Character.isUpperCase(nameFirstChar)) {
|
||||||
|
// First char is already uppercase, just use paramName.
|
||||||
|
param.vendorExtensions.put("x-export-param-name", param.paramName);
|
||||||
|
} else {
|
||||||
|
// It's a lowercase first char, let's convert it to uppercase
|
||||||
|
StringBuilder sb = new StringBuilder(param.paramName);
|
||||||
|
sb.setCharAt(0, Character.toUpperCase(nameFirstChar));
|
||||||
|
param.vendorExtensions.put("x-export-param-name", sb.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setExportParameterName(operation.queryParams);
|
||||||
|
setExportParameterName(operation.formParams);
|
||||||
|
setExportParameterName(operation.headerParams);
|
||||||
|
setExportParameterName(operation.bodyParams);
|
||||||
|
setExportParameterName(operation.cookieParams);
|
||||||
|
setExportParameterName(operation.optionalParams);
|
||||||
|
setExportParameterName(operation.requiredParams);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// recursively add import for mapping one type to multiple imports
|
||||||
|
List<Map<String, String>> recursiveImports = objs.getImports();
|
||||||
|
if (recursiveImports == null)
|
||||||
|
return objs;
|
||||||
|
|
||||||
|
ListIterator<Map<String, String>> listIterator = imports.listIterator();
|
||||||
|
while (listIterator.hasNext()) {
|
||||||
|
String _import = listIterator.next().get("import");
|
||||||
|
// if the import package happens to be found in the importMapping (key)
|
||||||
|
// add the corresponding import package to the list
|
||||||
|
if (importMapping.containsKey(_import)) {
|
||||||
|
listIterator.add(createMapping("import", importMapping.get(_import)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return objs;
|
||||||
|
}
|
||||||
|
|
||||||
private void setExportParameterName(List<CodegenParameter> codegenParameters) {
|
private void setExportParameterName(List<CodegenParameter> codegenParameters) {
|
||||||
for (CodegenParameter param : codegenParameters) {
|
for (CodegenParameter param : codegenParameters) {
|
||||||
char nameFirstChar = param.paramName.charAt(0);
|
char nameFirstChar = param.paramName.charAt(0);
|
||||||
|
@ -7,12 +7,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type {{classname}} struct {
|
type {{classname}} struct {
|
||||||
|
}
|
||||||
|
|
||||||
{{#operation}}
|
{{#operation}}
|
||||||
// {{httpMethod}} {{{basePathWithoutHost}}}{{{path}}}
|
// {{httpMethod}} {{{basePathWithoutHost}}}{{{path}}}{{#summary}}
|
||||||
// {{{summary}}}
|
// {{{.}}} {{/summary}}
|
||||||
{{#isDeprecated}}
|
{{#isDeprecated}}
|
||||||
// Deprecated
|
// Deprecated
|
||||||
{{/isDeprecated}}
|
{{/isDeprecated}}
|
||||||
{{nickname}} gin.HandlerFunc
|
func (api *{{classname}}) {{nickname}}(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
}{{/operations}}
|
{{/operations}}
|
||||||
|
@ -51,7 +51,8 @@ func DefaultHandleFunc(c *gin.Context) {
|
|||||||
type ApiHandleFunctions struct {
|
type ApiHandleFunctions struct {
|
||||||
{{#apiInfo}}{{#apis}}{{#operations}}
|
{{#apiInfo}}{{#apis}}{{#operations}}
|
||||||
// Routes for the {{classname}} part of the API
|
// Routes for the {{classname}} part of the API
|
||||||
{{classname}} {{classname}}{{/operations}}{{/apis}}{{/apiInfo}}
|
{{classname}} {{classname}}{{/operations}}{{/apis}}{{/apiInfo}}{{#webhooks}}{{#operations}}
|
||||||
|
{{classname}} {{classname}}{{/operations}}{{/webhooks}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRoutes(handleFunctions ApiHandleFunctions) []Route {
|
func getRoutes(handleFunctions ApiHandleFunctions) []Route {
|
||||||
|
@ -74,10 +74,12 @@ public class GoGinServerCodegenTest {
|
|||||||
|
|
||||||
DefaultGenerator generator = new DefaultGenerator();
|
DefaultGenerator generator = new DefaultGenerator();
|
||||||
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
|
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
|
||||||
files.forEach(File::deleteOnExit);
|
//files.forEach(File::deleteOnExit);
|
||||||
|
|
||||||
TestUtils.assertFileContains(Paths.get(output + "/go/routers.go"),
|
TestUtils.assertFileContains(Paths.get(output + "/go/routers.go"),
|
||||||
"NewPetPost");
|
"NewPetPost");
|
||||||
|
TestUtils.assertFileContains(Paths.get(output + "/go/api_default.go"),
|
||||||
|
" c.JSON(200, gin.H{\"status\": \"OK\"})");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,29 +14,62 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type PetAPI struct {
|
type PetAPI struct {
|
||||||
|
}
|
||||||
|
|
||||||
// Post /v2/pet
|
// Post /v2/pet
|
||||||
// Add a new pet to the store
|
// Add a new pet to the store
|
||||||
AddPet gin.HandlerFunc
|
func (api *PetAPI) AddPet(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Delete /v2/pet/:petId
|
// Delete /v2/pet/:petId
|
||||||
// Deletes a pet
|
// Deletes a pet
|
||||||
DeletePet gin.HandlerFunc
|
func (api *PetAPI) DeletePet(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Get /v2/pet/findByStatus
|
// Get /v2/pet/findByStatus
|
||||||
// Finds Pets by status
|
// Finds Pets by status
|
||||||
FindPetsByStatus gin.HandlerFunc
|
func (api *PetAPI) FindPetsByStatus(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Get /v2/pet/findByTags
|
// Get /v2/pet/findByTags
|
||||||
// Finds Pets by tags
|
// Finds Pets by tags
|
||||||
// Deprecated
|
// Deprecated
|
||||||
FindPetsByTags gin.HandlerFunc
|
func (api *PetAPI) FindPetsByTags(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Get /v2/pet/:petId
|
// Get /v2/pet/:petId
|
||||||
// Find pet by ID
|
// Find pet by ID
|
||||||
GetPetById gin.HandlerFunc
|
func (api *PetAPI) GetPetById(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Put /v2/pet
|
// Put /v2/pet
|
||||||
// Update an existing pet
|
// Update an existing pet
|
||||||
UpdatePet gin.HandlerFunc
|
func (api *PetAPI) UpdatePet(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Post /v2/pet/:petId
|
// Post /v2/pet/:petId
|
||||||
// Updates a pet in the store with form data
|
// Updates a pet in the store with form data
|
||||||
UpdatePetWithForm gin.HandlerFunc
|
func (api *PetAPI) UpdatePetWithForm(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Post /v2/pet/:petId/uploadImage
|
// Post /v2/pet/:petId/uploadImage
|
||||||
// uploads an image
|
// uploads an image
|
||||||
UploadFile gin.HandlerFunc
|
func (api *PetAPI) UploadFile(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,16 +14,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type StoreAPI struct {
|
type StoreAPI struct {
|
||||||
|
}
|
||||||
|
|
||||||
// Delete /v2/store/order/:orderId
|
// Delete /v2/store/order/:orderId
|
||||||
// Delete purchase order by ID
|
// Delete purchase order by ID
|
||||||
DeleteOrder gin.HandlerFunc
|
func (api *StoreAPI) DeleteOrder(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Get /v2/store/inventory
|
// Get /v2/store/inventory
|
||||||
// Returns pet inventories by status
|
// Returns pet inventories by status
|
||||||
GetInventory gin.HandlerFunc
|
func (api *StoreAPI) GetInventory(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Get /v2/store/order/:orderId
|
// Get /v2/store/order/:orderId
|
||||||
// Find purchase order by ID
|
// Find purchase order by ID
|
||||||
GetOrderById gin.HandlerFunc
|
func (api *StoreAPI) GetOrderById(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Post /v2/store/order
|
// Post /v2/store/order
|
||||||
// Place an order for a pet
|
// Place an order for a pet
|
||||||
PlaceOrder gin.HandlerFunc
|
func (api *StoreAPI) PlaceOrder(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,28 +14,61 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type UserAPI struct {
|
type UserAPI struct {
|
||||||
|
}
|
||||||
|
|
||||||
// Post /v2/user
|
// Post /v2/user
|
||||||
// Create user
|
// Create user
|
||||||
CreateUser gin.HandlerFunc
|
func (api *UserAPI) CreateUser(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Post /v2/user/createWithArray
|
// Post /v2/user/createWithArray
|
||||||
// Creates list of users with given input array
|
// Creates list of users with given input array
|
||||||
CreateUsersWithArrayInput gin.HandlerFunc
|
func (api *UserAPI) CreateUsersWithArrayInput(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Post /v2/user/createWithList
|
// Post /v2/user/createWithList
|
||||||
// Creates list of users with given input array
|
// Creates list of users with given input array
|
||||||
CreateUsersWithListInput gin.HandlerFunc
|
func (api *UserAPI) CreateUsersWithListInput(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Delete /v2/user/:username
|
// Delete /v2/user/:username
|
||||||
// Delete user
|
// Delete user
|
||||||
DeleteUser gin.HandlerFunc
|
func (api *UserAPI) DeleteUser(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Get /v2/user/:username
|
// Get /v2/user/:username
|
||||||
// Get user by user name
|
// Get user by user name
|
||||||
GetUserByName gin.HandlerFunc
|
func (api *UserAPI) GetUserByName(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Get /v2/user/login
|
// Get /v2/user/login
|
||||||
// Logs user into the system
|
// Logs user into the system
|
||||||
LoginUser gin.HandlerFunc
|
func (api *UserAPI) LoginUser(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Get /v2/user/logout
|
// Get /v2/user/logout
|
||||||
// Logs out current logged in user session
|
// Logs out current logged in user session
|
||||||
LogoutUser gin.HandlerFunc
|
func (api *UserAPI) LogoutUser(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
|
}
|
||||||
|
|
||||||
// Put /v2/user/:username
|
// Put /v2/user/:username
|
||||||
// Updated user
|
// Updated user
|
||||||
UpdateUser gin.HandlerFunc
|
func (api *UserAPI) UpdateUser(c *gin.Context) {
|
||||||
|
// Your handler implementation
|
||||||
|
c.JSON(200, gin.H{"status": "OK"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user