Code Monkey home page Code Monkey logo

chipi's Introduction

codecov Go Report Card

After being frustrated multiple times about the lack of easy way to generate an openapi doc directly from the code I created this library as an experiment and it went way further than I expected.

Other solutions

My main problem with the alternatives is simple: I don't want to maintain comments to describe my apis, in my experience those will slowly drift and become inaccurate. On the other hand if the code itself is the documentation it cannot technically drift or else it will not longer works.

My solution

The library is based on chi which is, so far, the best http router I found.

Each api endpoint is described by a structure:

// @tag
// pets
//
// @summary
// fetch a pet
//
// @deprecated
type GetPetRequest struct {
	response.ErrorEncoder
	response.JsonEncoder

	Path struct {
		// @description
		// Id is so great !
		// yeah !!
		//
		// @example
		// 789
		Id int32
	} `example:"/pet/5"`

	Query struct {
		Count    *int     `example:"2" description:"it counts... something ?"`
		Age      []int    `example:"[1,3,4]" style:"form" explode:"false" description:"line one\nline two" chipi:"required"`
		Names    []string `example:"[\"a\",\"b\",\"c\"]" style:"form" explode:"false" description:"line one\nline two"`
		OldField string   `chipi:"deprecated"`

		// @example
		// {"type": "point", "coordinates": [0.2, 9.0]}
		//
		// @description
		// # first
		// the location near the pet
		// ## second
		// some list of things:
		// - one
		// - two
		Location *Location
	}

	Header struct {
		ApiKey string
	}

	// @description
	// the returned pet
	Response Pet
}

func (r *GetPetRequest) Handle(ctx context.Context, w http.ResponseWriter) error {
	encoder := json.NewEncoder(w)
	err := encoder.Encode(&Pet{
		Id:    r.Path.Id,
		Name:  "Fido",
		Count: r.Query.Count,
	})

	fmt.Printf("names: %+v\n", r.Query.Names)
	fmt.Printf("location: %+v\n", r.Query.Location)

	return err
}

And you can use it like that:

err := api.Get(r, "/pet/{Id}", &GetPetRequest{})
if err != nil {
  panic(err)
}
  • Path is mandatory and describe the path parameters
  • Query is optional and will match query parameters (ex: "?count=4")
  • Body is optional and if present can be either a structure (json tags will be honored)
  • Response is also optional and define what is returned when eveything works well

Supported OpenAPI (v3.1) attributes

Structures

Special tags can be used on structure's fields to set specific behaviors:

  • ignored: the field will not show at all, triggered by:
    • json:"-"
    • chipi:"ignore"
  • read only: field only valid on read
    • chipi:"readonly"
  • write only: field only valid on write
    • chipi:"writeonly"
  • nullable: the field can be set to null
    • chipi:"nullable"
  • required
    • chipi:"required"
  • deprecated
    • chipi:"deprecated"
  • example
    • example:"field example"
  • description
    • description:"field description"

Path

reference

  • example [comment,tag]
  • description [comment,tag]
  • style [tag]
  • explode [tag]
  • deprecated [chipi-tag]

Query

reference

( same as path parameters )

  • required [chipi-tag]

Header

reference

( same as path parameters )

  • required [chipi-tag]

Body

reference

  • content-type [tag]
  • description [comment,tag]
  • required [chipi-tag]

Response

reference

  • description [comment,tag]
  • content-type [tag]

Caveats

This solution is not perfect and lack some features but I am sure a way to implement them can be found if needed:

  • no way to specify errors response, in my experience errors are often reported in a similar way for the whole api which may be documented as an introduction to the api.

  • no way to specify multiple mime type for body/response: that is a choice but what I need is a simple solution, I am not trying to solve every problems.

chipi's People

Contributors

schmurfy avatar zoov-xavier avatar fifteen-clement avatar fifteen-gustutu avatar mathieuzoov avatar misfits09 avatar nhoway 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.