1

I'm using openapi-generator-maven-plugin to generate code from YAML documentation.

openapi: 3.0.3
info:
  title: Example API Doc
  description: Those are example endpoints.
  termsOfService: https://localhost:8080/termsOfService
  license:
    name: No license
    url: https://localhost:8080/license
  version: 1.0-SNAPSHOT
servers:
- url: https://localhost:8080/
- url: http://localhost:8080/
tags:
- name: example
  description: Example Tag
paths:
  /example:
    get:
      tags:
      - example
      summary: Example GET request
      description: Send example GET request
      operationId: exampleGetRequest
      responses:
        200:
          description: Successful operation
          content: {}
        400:
          description: Example Bad Request
          content: {}
        404:
          description: Example Not Found
          content: {}

This is how I configure maven plugin to generate interfaces:

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>4.1.1</version>
  <configuration>
    <generatorName>spring</generatorName>
    <inputSpec>${project.basedir}/src/main/resources/exampleApi.yaml</inputSpec>
    <apiPackage>com.example.openapi.generated.api</apiPackage>
    <modelPackage>com.example.openapi.generated.models</modelPackage>
    <generateSupportingFiles>false</generateSupportingFiles>
    <output>${project.basedir}</output>
    <configOptions>
      <dateLibrary>java8</dateLibrary>
      <java8>true</java8>
      <interfaceOnly>true</interfaceOnly>
    </configOptions>
  </configuration>
</plugin>

This plugin works well and I get interface like this

@Api(value = "example", description = "the example API")
public interface ExampleApi {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    @ApiOperation(value = "Example GET request", nickname = "exampleGetRequest", notes = "Send example GET request", tags = {"example",})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successful operation"),
            @ApiResponse(code = 400, message = "Example Bad Request"),
            @ApiResponse(code = 404, message = "Example Not Found")})
    @RequestMapping(value = "/example", method = RequestMethod.GET)
    default ResponseEntity<Void> exampleGetRequest() {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }
}

I'm implementing my interface with this class:

@RestController
public class ExampleApiImpl implements ExampleApi {

    @Override
    public ResponseEntity<Void> exampleGetRequest() {
        return ResponseEntity.ok().build();
    }
}

I've checked the solution and I've got HTTP status 200 when I perform api call, but when I try to access swagger-ui page, this API endpoint is not documented. Is there any way to configure OpenAPI UI or SwaggerUI to point to my yaml documentation?

SwaggerUI screen enter image description here

Kacper Fleszar
  • 156
  • 1
  • 8
  • Hi, i am having the same issue. Do you found any solution for this? – denfri.dev Sep 03 '20 at 08:14
  • Hi, I'm not sure if it solve the issue, because I've changed a lot of things from that moment. Try to annotate your implementation class as `@Controller`, not the `@RestController`. Also my interface now has no default methods, all the endpoint method declarations all 'abstract' as you can say it in interfaces. If it works for you, put an answer here for others. – Kacper Fleszar Sep 03 '20 at 09:59
  • Hi, if i annotate the implementation class as @Controlller swagger ui displays "No operations defined in spec". I set both settings "interfaceOnly" and "skipDefaultInterface" to "true". – denfri.dev Sep 03 '20 at 13:24

1 Answers1

0

if it's still topical.

Looks like you use OpenAPI UI for Swagger 3 annotations, but OpenApi generator can create only Swagger 2 annotations: Open API code generator Maven plugin uses old Swagger 2 annotations instead of Swagger 3 annotations

Solution #1 - use Swagger v2 UI. Remove openApi dependency and use io.springfox:springfox-swagger2:{version} for UI page.

Solution #2 - override mustache templates for generation v3 annotations.

tripleee
  • 175,061
  • 34
  • 275
  • 318
M.Surnyk
  • 33
  • 4