swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.37k stars 2.17k forks source link

Webhooks not rendered in generated spec #4673

Closed mark-roy closed 1 month ago

mark-roy commented 4 months ago

I have a project that I have been successfully generating openapi.json using Swagger annotations v2.2.22. I've taken the @Webhooks example from modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/petstore/WebHookResource.java and added it to my project, but have not been able to get webhooks rendered in the spec file.

I've debugged the decompiled code to this point in SpecFilter, where the resourcePath is "webhook2" (from the example hook). The code then does a 'get' on that resource path and gets null on return, which it passes on to filterPathItem. Eventually this results in a NullPointerException.

            if (filteredOpenAPI.getWebhooks() != null) {
                Iterator var18 = filteredOpenAPI.getWebhooks().keySet().iterator();

                while(var18.hasNext()) {
                    String resourcePath = (String)var18.next();
                    pathItem = (PathItem)filteredOpenAPI.getPaths().get(resourcePath);
                    filteredPathItem = this.filterPathItem(filter, pathItem, resourcePath, params, cookies, headers);
                    PathItem clonedPathItem = this.cloneFilteredPathItem(filter, filteredPathItem, resourcePath, params, cookies, headers, allowedTags, filteredTags);
                    if (clonedPathItem != null && !clonedPathItem.readOperations().isEmpty()) {
                        clone.addWebhooks(resourcePath, clonedPathItem);
                    }
                }
            }

Do I need to define a @Path("webhook2") somewhere? The example code doesn't seem to.

micryc commented 3 months ago

Hi @mark-roy may I ask you what version of openapi spec are you generating ?

mark-roy commented 3 months ago

@micryc Hi Michal; I see now that a version 3.0.1 spec is being generated. How do I cause it to generate version 3.1?

Thanks

micryc commented 1 month ago

Hi @mark-roy sorry for late response, If problem still exists for you here are some details to generate OAS v3.1 Depending on what tool exactly you use to generate the spec:

Swagger-Maven-Plugin: You can achieve that by adding configuration property:

<plugin>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>2.2.22</version>
    <configuration>
        <outputFileName>webhook</outputFileName>
        <outputPath>src/main/java/org/example/generatedtest</outputPath>
        <outputFormat>YAML</outputFormat>
        <openapi31>true</openapi31>  set openapi to 3.1
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>resolve</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Exposing definition via API You can achieve that by adding configuration to your main class

@ApplicationPath("/webhooktest")
public class RestApplication extends ResourceConfig {

    public RestApplication() {
        OpenAPI openAPI = new OpenAPI();
        openAPI.info(new Info().title("My API ").version("1.0"));

        SwaggerConfiguration oasConfig = new SwaggerConfiguration()
                .openAPI(openAPI)
                .readAllResources(true)
                .openAPI31(true);  //  turn on  OpenApi 3.1

        BaseOpenApiResource openApiResource = new OpenApiResource()
                .openApiConfiguration(oasConfig);
        register(openApiResource);

    }
}

I am closing this ticket for now, If you will still have any problems feel free to reopen with more details