Giter Club home page Giter Club logo

goag's Introduction

Goag - Golang OpenAPIv3 Generator

This tool generates boilerplate code for the http handling. It handles the parsing of queries, headers or json bodies.

It makes impossible to return undocumented type/response by endpoint.

Example

var db interface {
    GetPet(_ context.Context, id string) (Pet, error)
}

func ExampleAPI_petsStore() {
    api := &API{
        ShowPetByIDHandler: func(ctx context.Context, r ShowPetByIDRequest) ShowPetByIDResponse {
            req, err := r.Parse()
            if err != nil {
                return NewShowPetByIDResponseDefaultJSON(http.StatusBadRequest, Error{
                    Code:    400,
                    Message: fmt.Sprintf("Bad request: %v", err),
                })
            }

            out, err := db.GetPet(r.HTTP().Context(), req.Path.PetID)
            if err != nil {
                return NewShowPetByIDResponseDefaultJSON(http.StatusInternalServerError, Error{
                    Code:    500,
                    Message: fmt.Sprintf("Internal server error: %v", err),
                })
            }

            return NewShowPetByIDResponse200JSON(out)
        },
        // ...
    }

    _ = http.ListenAndServe(":8080", api)
}
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

goag's People

Contributors

vkd avatar

Watchers

 avatar  avatar  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.