GithubHelp home page GithubHelp logo

go-oas3's Introduction

Go OpenAPI v3 server codegenerator

The purpose of this project is to generate a clean server boilerplate code from openapi v3 specification. The generated code based on github.com/go-chi/chi/v5 router. Generator goes over all paths and components and generates Go structs and stubs.

Key ideas:

  • Stubs take over the logic of parsing the request.
  • Response builders encapsulated logic that doesn't allow you to respond differently from your specification.

Take a note that path stubs generation relies on the first tag from your paths.

Installation

GO111MODULE=on go get github.com/mikekonan/[email protected]

Program arguments

Usage of go-oas3:
  -componentsPackage string
  -componentsPath string
  -package string
  -path string
  -swagger-addr string
    	 (default "swagger.yaml")
  -authorization string 
    a list of comma-separated key:value pairs to be sent as headers alongside each http request
  -prioritize-x-go-type
    by default, if both properties and x-go-type is provided, the generator will use properties.
    this flag will make generator prioritize x-go-type over properties.

Example

Run with: go-oas3 -swagger-addr https://raw.githubusercontent.com/mikekonan/go-oas3/v1.0.49/example/swagger.yaml -package example -path ./example The result generated boilerplate and its client you can see at ./example.

OpenAPI features

Required fields

Path, query, component, header required fields are supported.

Security

Security schemas for http, apikey (header/cookie).

Cookie

Response header Set-Cookie supported. Cookie in request supported via security schema only.

Validation

Types validation supports following data types:

  • string: minLength, maxLength
  • number, integer: minimum, maximum, exclusiveMinimum, exclusiveMaximum

Custom types

Generator supports few swagger types for components.

openapi type go type
uuid github.com/google/uuid.UUID
iso4217-currency-code github.com/mikekonan/go-types/v2/currency.Code
iso3166-alpha-2 github.com/mikekonan/go-types/v2/country.Alpha2Code
iso3166-alpha-3 github.com/mikekonan/go-types/v2/country.Alpha3Code

Extentions:

Specify a go type with:

    Component:
      properties:
        metadata:
          type: object
          x-go-type: encoding/json.RawMessage

Forcing pointer usage for the field:

    Component:
      properties:
        amount:
          type: int
          x-go-pointer: true

Specify a regex to match a string:

    Parameter:
      description: Parameter
      in: header
      name: parameter name
      required: true
      schema:
        type: string
        x-go-regex: ^[.?\d]+$

If you want to use your specific type(it has to declare function Parse{TYPENAME} ({TYPENAME}, error)) in query/path/header params:

    TYPENAME:
      type: string
      x-go-type: githubrepo/lib/pkg.{TYPENAME}
      x-go-type-string-parse: githubrepo/lib/pkg.Parse{TYPENAME}

If you want to have a specific go map you can also use x-go-type to specify a key. It works only if additionalProperties specified.

    ResponseBody:
      type: object
      additionalProperties:
        items:
          $ref: '#/components/schemas/objects.Type'
        type: array
      x-go-type: githubrepo/objects.SomeType

Use x-go-string-timmable key If you would like to trim spaces before validation. It works only for string type

    ResponseBody:
      properties:
        title:
          type: string
          x-go-string-trimmable: true

If you want to add omitempty tag you can also use x-go-omitempty

    ResponseBody:
      properties:
        title:
          type: string
          x-go-omitempty: true

Have a question or need some functionality?

Feel free to discuss it or do a PR.

Contribution

Go OpenAPI v3 server codegenerator uses https://github.com/dave/jennifer. Using https://github.com/aloder/tojen is suggested way to generate jennifer code.

go-oas3's People

Contributors

mikekonan avatar sergeydobrodey avatar valichek avatar asechiho avatar glebbeloded avatar vadim-mve avatar konstantsiy avatar lstmix 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.