Skip to content

[Notes Internal] Tech Notes Swagger integration

Richard Hightower edited this page Aug 1, 2015 · 1 revision

This will be in the next release of QBit. We have come pretty far along.

These are just notes and they are outdated.

Swagger integration is not done. It is in progress.. We are working on some swagger integration. We are not there yet. But we are working on it. The idea being that we can generate a JSON file that can be imported into Swagger UI and then generate clients for endpoints.

Later we will most likely add support for: http://raml.org/

It is not done yet.

Update....

Given these....

@RequestMapping("/sample/service")
public class SampleService {


    @RequestMapping("/simple1/")
    public String simple1() {
        return "simple1";
    }


    @RequestMapping("/call1/foo/{arg4}/{2}")
    public String method1(@RequestParam("arg1") final String arg1,
                          @HeaderParam("arg2") final int arg2,
                          @PathVariable final float arg3,
                          @PathVariable("arg4") final double arg4) {


        return sputs(arg1, arg2, arg3, arg4);
    }


    //"/call2/{2}/{arg4}")
    public String method2(final String arg1,
                          final int arg2,
                          final float arg3,
                          final double arg4) {

        return sputs(arg1, arg2, arg3, arg4);
    }


    @RequestMapping("/simple2/path/")
    public String simple2(@RequestParam("arg1") final String arg1) {
        return "simple2";
    }



    @RequestMapping("/add/dept/")
    public Department addDepartment(final Department department) {

        return department;
    }


}

package io.advantageous.qbit.meta.swagger;

import java.util.List;

public class Department {
    List<Employee> employeeList;
    Employee[] employeeArray;
}

package io.advantageous.qbit.meta.swagger;

public class Phone {

    String countryCode;
    String areaCode;
    String digits;
}


package io.advantageous.qbit.meta.swagger;

public class Employee {

    Phone phone;
    String name;
    int age;
    long iq;

    byte byteA;
    Byte byteB;
    byte[] byteC;
    Byte[] byteD;

    float floatA;
    Float floatB;

    double dA;
    Double dB;

}

We are able to generate these:

{
    "swagger": "2.0",
    "info": {
        "contact": {
            "name": "Rick Hightower",
            "url": "https://github.com/advantageous/qbit",
            "email": "rick@rick.com"
        },
        "license": {
            "name": "APACHE 2",
            "url": "https://github.com/advantageous/qbit/blob/master/License"
        }
    },
    "host": "localhost:9090",
    "basePath": "/servicesEngine",
    "schemes": [
        "http",
        "https",
        "wss",
        "ws"
    ],
    "consumes": [
        "application/json"
    ],
    "definitions": {
        "Department": {
            "properties": {
                "employeeList": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Employee"
                    }
                },
                "employeeArray": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Employee"
                    }
                }
            }
        },
        "Employee": {
            "properties": {
                "phone": {
                    "$ref": "#/definitions/Phone"
                },
                "name": {
                    "type": "string"
                },
                "age": {
                    "type": "integer",
                    "format": "int32"
                },
                "iq": {
                    "type": "integer",
                    "format": "int64"
                },
                "byteA": {
                    "type": "string",
                    "format": "byte"
                },
                "byteB": {
                    "type": "string",
                    "format": "byte"
                },
                "byteC": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "format": "byte"
                    }
                },
                "byteD": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "format": "byte"
                    }
                },
                "floatA": {
                    "type": "number",
                    "format": "float"
                },
                "floatB": {
                    "type": "number",
                    "format": "float"
                },
                "dA": {
                    "type": "number",
                    "format": "double"
                },
                "dB": {
                    "type": "number",
                    "format": "double"
                }
            }
        },
        "Phone": {
            "properties": {
                "countryCode": {
                    "type": "string"
                },
                "areaCode": {
                    "type": "string"
                },
                "digits": {
                    "type": "string"
                }
            }
        }
    },
    "produces": [
        "application/json"
    ],
    "paths": {
        "/call1/foo/{arg4}/{2}": {
            "get": {
                "operationId": "method1",
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "arg1",
                        "in": "query",
                        "type": "string"
                    },
                    {
                        "name": "arg2",
                        "in": "header",
                        "type": "integer"
                    },
                    {
                        "in": "THIS QBIT FEATURE URI POSITIONAL PARAM IS NOT SUPPORTED BY SWAGGER",
                        "type": "number"
                    },
                    {
                        "name": "arg4",
                        "in": "path",
                        "type": "number"
                    }
                ],
                "responses": {
                    "200": {
                        "schema": {
                            "type": "string"
                        }
                    }
                }
            }
        },
        "/simple2/path/": {
            "get": {
                "operationId": "simple2",
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "arg1",
                        "in": "query",
                        "type": "string"
                    }
                ],
                "responses": {
                    "200": {
                        "schema": {
                            "type": "string"
                        }
                    }
                }
            }
        },
        "/simple1/": {
            "get": {
                "operationId": "simple1",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {
                        "schema": {
                            "type": "string"
                        }
                    }
                }
            }
        },
        "/add/dept/": {
            "get": {
                "operationId": "addDepartment",
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "body",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/Department"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "schema": {
                            "$ref": "#/definitions/Department"
                        }
                    }
                }
            }
        }
    }
}

Older notes...

If you had a service like this:

@RequestMapping("/sample/service")
public class SampleService {


    @RequestMapping("/simple1/")
    public String simple1() {
        return "simple1";
    }


    @RequestMapping("/call1/foo/{arg4}/{2}")
    public String method1(@RequestParam("arg1") final String arg1,
                          @HeaderParam("arg2") final int arg2,
                          @PathVariable final float arg3,
                          @PathVariable("arg4") final double arg4) {


        return sputs(arg1, arg2, arg3, arg4);
    }


    //"/call2/{2}/{arg4}")
    public String method2(final String arg1,
                          final int arg2,
                          final float arg3,
                          final double arg4) {

        return sputs(arg1, arg2, arg3, arg4);
    }


    @RequestMapping("/simple2/path/")
    public String simple2(@RequestParam("arg1") final String arg1) {
        return "simple2";
    }


}

Then we can generate a swagger file as so.

{
    "swagger": "2.0",
    "info": {
        "contact": {},
        "license": {}
    },
    "host": "localhost",
    "basePath": "/servicesEngineEngine",
    "schemes": [
        "http",
        "https",
        "wss",
        "ws"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "paths": {
        "/call1/foo/{arg4}/{2}": {
            "get": {
                "operationId": "method1",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {}
                }
            }
        },
        "/simple2/path/": {
            "get": {
                "operationId": "simple2",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {}
                }
            }
        },
        "/method2": {
            "get": {
                "operationId": "method2",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {}
                }
            }
        },
        "/simple1/": {
            "get": {
                "operationId": "simple1",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {}
                }
            }
        }
    }
}

We really just started but there are a few API tools we would like to produce. Swagger being just one.

QBit has its own meta that we use to convert to swagger.

See the class io.advantageous.qbit.meta.swagger.MetaTransformerFromQbitMetaToSwagger to see progress.

Update...

We are able to convert classes into defintions.

Give this.

    public static class Phone {

        String countryCode;
        String areaCode;
        String digits;
    }

    public static class Employee {

        Phone phone;
        String name;
        int age;
        long iq;

        byte byteA;
        Byte byteB;
        byte[] byteC;
        Byte[] byteD;

        float floatA;
        Float floatB;

        double dA;
        Double dB;

    }

    public static class Department {
        List<Employee> employeeList;
        Employee[] employeeArray;
    }

We can produce this set of definitions.

{
    "Department": {
        "properties": {
            "employeeList": {
                "type": "array",
                "items": {
                    "$ref": "#/definitions/Employee"
                }
            },
            "employeeArray": {
                "type": "array",
                "items": {
                    "$ref": "#/definitions/Employee"
                }
            }
        }
    },
    "Employee": {
        "properties": {
            "phone": {
                "type": "array",
                "items": {
                    "$ref": "#/definitions/Phone"
                }
            },
            "name": {
                "type": "string"
            },
            "age": {
                "type": "integer",
                "format": "int32"
            },
            "iq": {
                "type": "integer",
                "format": "int64"
            },
            "byteA": {
                "type": "string",
                "format": "byte"
            },
            "byteB": {
                "type": "string",
                "format": "byte"
            },
            "byteC": {
                "type": "array",
                "items": {
                    "type": "string",
                    "format": "byte"
                }
            },
            "byteD": {
                "type": "array",
                "items": {
                    "type": "string",
                    "format": "byte"
                }
            },
            "floatA": {
                "type": "number",
                "format": "float"
            },
            "floatB": {
                "type": "number",
                "format": "float"
            },
            "dA": {
                "type": "number",
                "format": "double"
            },
            "dB": {
                "type": "number",
                "format": "double"
            }
        }
    },
    "Phone": {...}
}

Tutorials

__

Docs

Getting Started

Basics

Concepts

REST

Callbacks and Reactor

Event Bus

Advanced

Integration

QBit case studies

QBit 2 Roadmap

-- Related Projects

Kafka training, Kafka consulting, Cassandra training, Cassandra consulting, Spark training, Spark consulting

Clone this wiki locally