Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPI 3.1.0 Add webhooks support #17174

Merged
merged 5 commits into from
Dec 11, 2023
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 @@ -30,6 +30,7 @@
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.model.WebhooksMap;

import java.io.File;
import java.util.List;
Expand Down Expand Up @@ -217,6 +218,8 @@ public interface CodegenConfig {

OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels);

WebhooksMap postProcessWebhooksWithModels(WebhooksMap objs, List<ModelMap> allModels);

Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs);

void postProcessModelProperty(CodegenModel model, CodegenProperty property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CodegenConstants {
public static final String SUPPORTING_FILES = "supportingFiles";
public static final String MODEL_TESTS = "modelTests";
public static final String MODEL_DOCS = "modelDocs";
public static final String WEBHOOKS = "webhooks";
public static final String API_TESTS = "apiTests";
public static final String API_DOCS = "apiDocs";

Expand Down Expand Up @@ -296,6 +297,7 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,

public static final String GENERATE_APIS = "generateApis";
public static final String GENERATE_API_DOCS = "generateApiDocs";
public static final String GENERATE_WEBHOOKS = "generateWebhooks";

public static final String GENERATE_API_TESTS = "generateApiTests";
public static final String GENERATE_API_TESTS_DESC = "Specifies that api tests are to be generated.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.model.WebhooksMap;
import org.openapitools.codegen.serializer.SerializerUtils;
import org.openapitools.codegen.templating.MustacheEngineAdapter;
import org.openapitools.codegen.templating.mustache.*;
Expand Down Expand Up @@ -977,6 +978,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
return objs;
}

// override with any special post-processing
@Override
@SuppressWarnings("static-method")
public WebhooksMap postProcessWebhooksWithModels(WebhooksMap objs, List<ModelMap> allModels) {
return objs;
}

// override with any special post-processing
@Override
@SuppressWarnings("static-method")
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.openapitools.codegen.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WebhooksMap extends HashMap<String, Object> {
public OperationMap getWebhooks() {
return (OperationMap) get("operations");
}

public void setWebhooks(OperationMap objs) {
put("operations", objs);
}

@SuppressWarnings("unchecked")
public List<Map<String, String>> getImports() {
return (List<Map<String, String>>) get("imports");
}

public void setImports(List<Map<String, String>> imports) {
put("imports", imports);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@ type ApiHandleFunctions struct {
}

func getRoutes(handleFunctions ApiHandleFunctions) []Route {
return []Route{
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
return []Route{ {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
{
"{{operationId}}",
http.Method{{httpMethod}},
"{{{basePathWithoutHost}}}{{{path}}}",
handleFunctions.{{classname}}.{{operationId}},
},{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
},{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}{{#webhooks}}{{#operations}}{{#operation}}
// webhook handler (adjust path accordingly)
{
"{{operationId}}",
http.Method{{httpMethod}},
"{{{basePathWithoutHost}}}{{{path}}}",
handleFunctions.{{classname}}.{{operationId}},
},{{/operation}}{{/operations}}{{/webhooks}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -4808,4 +4809,18 @@ void testIsXML() {
Assert.assertTrue(codegen.isXmlMimeType("application/xml"));
Assert.assertTrue(codegen.isXmlMimeType("application/rss+xml"));
}

@Test
public void testWebhooks() throws IOException {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_1/webhooks.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

Operation operation = openAPI.getWebhooks().get("newPet").getPost();
CodegenOperation co = codegen.fromOperation("newPet", "get", operation, null);

Assert.assertEquals(co.path, "/newPet");
Assert.assertEquals(co.operationId, "newPetGet");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,25 @@ public void verifyGoMod() throws IOException {
"require github.com/gin-gonic/gin v1.9.1");
}

@Test
public void webhooks() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("go-gin-server")
.setGitUserId("my-user")
.setGitRepoId("my-repo")
.setPackageName("my-package")
.setInputSpec("src/test/resources/3_1/webhooks.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);

TestUtils.assertFileContains(Paths.get(output + "/go/routers.go"),
"NewPetPost");
}

}
34 changes: 34 additions & 0 deletions modules/openapi-generator/src/test/resources/3_1/webhooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: 3.1.0
info:
title: Webhook Example
version: 1.0.0
# Since OAS 3.1.0 the paths element isn't necessary. Now a valid OpenAPI Document can describe only paths, webhooks, or even only reusable components
webhooks:
# Each webhook needs a name
newPet:
# This is a Path Item Object, the only difference is that the request is initiated by the API provider
post:
requestBody:
description: Information about a new pet in the system
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
responses:
"200":
description: Return a 200 status to indicate that the data was received successfully

components:
schemas:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
3 changes: 1 addition & 2 deletions samples/server/petstore/go-gin-api-server/go/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ type ApiHandleFunctions struct {
}

func getRoutes(handleFunctions ApiHandleFunctions) []Route {
return []Route{

return []Route{
{
"AddPet",
http.MethodPost,
Expand Down
Loading