Giter VIP home page Giter VIP logo

swagger-grails4's Introduction

swagger-grails4

Grails4 Plugin to automatically generate swagger/OpenAPI-v3 REST API documents.

1. Usage

1.1. Requirements

  1. Grails v4 or greater

  2. Java 1.8

  3. Groovy 2.5

1.2. General Usage steps

1.2.1. Specify gradle dependence

repositories {
  maven { url "https://dl.bintray.com/bobyang/plugins" }
  // or jcenter()
}
dependencies {
  implementation 'swagger.grails4:swagger-grails4:0.0.1'
}

1.2.2. Add @ApiDoc annotations

Add @ApiDoc annotations to Controller and Command/Domain classes any Schema classes you want plugin to automatically extract your comments as OpenAPI descriptions.

Add @ApiDoc to controller:

@ApiDoc(tag = {
    description "User API"
})
class UserController {
}
Add @ApiDoc to action methods:
@ApiDoc(operation = {
    summary "Create User"
    description "Create a new user"
    responses "200": {
        content "default": {
            description "success response"
            schema MyApiResponse
        }
    }
})
def createUser(UserCommand command) {
}

The plugin will automatically extract comments or annotation in the command class, if you add @ApiDoc annotation to the class.

Add @ApiDoc annotation to the command class or domain classes.
@ApiDoc("The command contains User properties")
class UserCommand implements Validateable{
    /**
     * The name of user in comments.
     */
    String username
}

1.2.3. API endpoint and html document URL

Now start your REST api grails application and visit "http://<host:port>/api", you can see swagger-ui document.

Visit "http://<host:port>/api/doc", you can get json object of document.

1.2.4. More controls

You can override comments using @ApiDoc in the fields
@ApiDoc("The command contains User properties")
class UserCommand implements Validateable{
    /**
     * The name of user in comments.
     */
    @ApiDoc("The name of user")
    String username
}

If the action parameters is primitive types, you can document them like this.

Using OpenAPI Parameter model to document parameters
@ApiDoc(operation = {
    summary "Login"
    description "Login with user name and password"
    parameters([{
                    name "username"
                    description "User Name"
                    inType "query"
                    schema { type "string" }
                }, {
                    name "password"
                    description "Password"
                    inType "query"
                    schema { type "string" }
                }])
})
def login(String password, String username) {
}

Every parameter in the 'annotation closure' (Groovy concept) is a Parameter object, so you can use any 'primitive type properties' that Parameter object has, complex object properties needs special processing, maybe not available.

1.2.4.1. Advanced Response document

You can change the properties of Response schema, this is convenient if your api response has payload that changed in action methods.

given response class like this:
@ApiDoc("A test rest api response class")
class RestApiResponse {
    /**
     * Error code
     */
    int code
    /**
     * Message
     */
    String msg
    /**
     * Return payload
     */
    Object info
}
override properties of a response class because this action return UserCommand in the info property
@ApiDoc(operation = {
    summary "Login"
    description "Login with user name and password"
    responses "200": {
        content "default": {
            description "success response"
            schema RestApiResponse, properties: [info: UserCommand]
        }
    }
})
def login(LoginCommand loginCommand) {
}

You can even totally define schema in the annotation closure.

define schema in annotation closure
@ApiDoc(operation = {
    summary "Create User"
    description "Create a new user"
    responses "200": {
        content "default": {
            description "success response"
            schema {
                name "CustomSchema"
                type "string"
                description "The customized json response"
            }
        }
    }
})
def createUser(UserCommand command) {
}

You can specify multiple "Status Code" and content MIME in responses.

specify multiple "Status Code"
@ApiDoc(operation = {
    summary "List Users"
    description "List users, support query and paging parameters"
    responses "200": {
        content "default": {
            description "success response"
            schema RestApiResponse
        }
    }, "201": {
        content "default": {
            description "success response with 201"
            schema UserCommand
        }
    }
})
def index() {
}
specify multiple "MIME" content
@ApiDoc(operation = {
    summary "List Users"
    description "List users, support query and paging parameters"
    responses "200": {
        content "default": {
            description "success response"
            schema RestApiResponse
        }, "text/xml": {
            description "success response with 201"
            schema UserCommand
        }
    }
})
def index() {
}

2. Features

  • Automatically build operations from grails controllers and UrlMapping.

  • Automatically extract Schema from any classes with @ApiDoc annotation.

  • Automatically extract comments of fields to build descriptions of properties.

  • Automatically create description of values of Enum, if there is an id property then show id value in descriptions.

  • Automatically create element Schema of array.

  • Correctly process reference cycle Because swagger-ui will hang-up if there is a cycle referencing in schemas, so we will not reference existed schema in array schemas items.

  • Hide api doc in production environment.

  • Automatically generate response object document.

  • 'properties' of response Schema can be customized

  • TODO: Can handle inherited trait properties and plain class properties.

  • TODO: recognize GORM association properties.

If you need some more features please feel free to submit an issue with 'enhancement' label, any suggestions are welcome.

We wish this plugin can save your time to write tedious api documentations.

Enjoy Grails REST API document with 'swagger-grails4'!

3. About Author

We are Beijing Telecwin Co.Ltd. a company provides software develop services and develop SaaS systems for e-commercial merchants in PWA such as weixin mini-programs and Mobile-Application such as Android/iOS app.

We located in Beijing China, if you are interested in our service and products please feel free to contact us in [email protected].

swagger-grails4's People

Contributors

yangbo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.