bcgov / Swagger-Codegen-Extension

Apache License 2.0
4 stars 6 forks source link

Swagger Codegen Extension

Introduction


The Swagger Codegen Extension contains 3 code generator extensions that transform an OpenAPI specification into application code for:

All 3 code generators are intended to be used with applications deployed to OpenShift, however they are also compatible with applications deployed to traditional hosting environments.

The generated code somewhat extends the initial purpose of Swagger codegen, which was to generate the REST client and server code for a public API. The code generated with these generators are intended for use in generating the internal API of a web application - the REST interface between the front end and back end. In doing the backend code generation, the models for the code are generated, enabling the API Server Framework (e.g. .NET Core, Django, etc.) Object Relation Mapper (ORM) component (e.g. Entity-Framework for .NET Core) to generate the database model for the application. Thus, from a swagger definition, all of the code with checkmarks can be generated as seen below. The pencils indicate where manual code is required. Note that the ORM and database model code is not generated by the generator from this project - it's handled by the ORM component of the application framework.

Swagger Codegen Extension - Generated Code

The codegen extension is being developed as an open source solution.

Build Prerequisites

Building the Generator

The resulting jar file can then be used with the Swagger.io Codegen utility.

Project Code Generation Usage


Once you have the jar required for your project, copy several files from this project to the place in your application project where you will generate the code. We recommend creating a /APISpec folder in your project where you maintain the swagger file and you run the code generator. The needed files are:

You also need to get the swagger-codegen-cli.jar from the official Swagger Codegen project. To get the file use the steps provided here.

NOTE: Be sure to check back here (and the Swagger Codegen) to see if this code generator has evolved in a useful way for your project and if so, copy the latest code from here to your project.

Once you have downloaded the files, we recommend that you edit the extcodegen.bat and config files to set the required values for your project. The extcodegen.bat file documents the variables you can set to tune the behaviour of the generation for your project. You can use command line parameters to run the script, but that can be tedious.

There are two types of generated code from the generator:

The trick is, of course, knowing as you iterate on the API design what manual code changes are needed to address changes in the API. More on that below.

Iterative Generation

On first use, you will copy most of the generated code into your application, and manually update some of the generated code. If possible, it would be good to iterate on the API without manually updating the code until you have a significant chunk of the API completed.

As your OpenAPI (swagger) definition evolves, we recommend the following approach to code generation once you have manually edited the required generated code:

The following provides information by tech stack about generated code that can be used as is, and what code needs to be manually maintained in the application.

Django

The Django generator is meant to be used to generate the skeleton for an application that uses the Django REST API libraries. It will generate the following files:

The following files will be used "as is" and so can be safely copied into the target project folder after generation:

ASP .NET Core

The .net core generate is intended to be used to build a .net core 1.1 MVC application.

It generates the following:

The .NET Core generator does not automatically implement CRUD operations.

The files in the following folders will be used "as is" and so can be safely copied into the target project folder after generation (where "API" is updated based on your generator config.json file)

The src/API/Services.Impl are just stubs of the necessary implementation, and these are what are needed to be manually maintained. The following is some guidance on that:

Java Fuse

The Java fuse generator creates a CXF / CDI FUSE skeleton that is suitable for creating a back end service or microservice. The skeleton will run on its own however is intended to be edited to implement functionality.

The Java fuse generator creates the following:

The Java Fuse generator does not create database objects at this time.

Swagger-Codegen-Extension Development


The Swagger-Codegen-Extension is composed of a set of Java classes, one for each implemented tech stack.

Each implemented tech stack also has a set of Mustache templates which are used to render the code.

Contribution


Please report any issues.

Pull requests are always welcome.

If you would like to contribute, please see our contributing guidelines.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

Copyright 2017 Province of British Columbia

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Maintenance

This repository is maintained by BC Ministry of Transportation. Click here for a complete list of our repositories on GitHub.