Skip to content

About

libopenapi is a fully featured, high performance OpenAPI 3.2, 3.1, 3.0, Overlays and Arazzo parser, library, validator and toolkit for go applications.

Topics

Resources

Security policy

Stars

891 stars

Watchers

6 watching

Forks

Latest commit

 

History

1,872 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libopenapi

libopenapi - enterprise grade OpenAPI tools for golang.

Pipeline GoReportCard codecov discord Docs

libopenapi has full support for OpenAPI 3, 3.1 and 3.2. It can handle the largest and most complex specifications you can think of.

Overlays and Arazzo are also fully supported.


Sponsors & users

If your company is using libopenapi, please considering supporting this project, like our very kind sponsors:

scalar'

scalar

apideck'

apideck


Come chat with us

Need help? Have a question? Want to share your work? Join our discord and come say hi!

Check out the libopenapi-validator

Need to validate requests, responses, parameters or schemas? Use the new libopenapi-validator module.

Documentation

See all the documentation at https://pb33f.io/libopenapi/

Comparing standalone JSON Schemas

Use the jsonschema package to compare Draft 2020-12 schemas. Pass the source name so relative references resolve against the correct file. Pass true to Load to select this dialect for an unmarked schema or a boolean root.

before, err := jsonschema.Load(original, "before/customer.json", nil, true)
if err != nil {
    return err
}
defer before.Release()
after, err := jsonschema.Load(modified, "after/customer.json", nil, true)
if err != nil {
    return err
}
defer after.Release()
changes, err := jsonschema.Compare(before, after, nil)

Compare returns a nil report for equal schemas. For a collection, use jsonschema.LoadCollection with a map of relative filenames to source bytes and the directory for that snapshot. It registers each member before resolving references. Each change carries its resource ID, local JSONPath, and source locations. Releases and breaking-rule overrides work the same for either input.

The third argument to Compare holds explicit overrides only. A rule replaces the verdict for its keyword in both directions, so pass nil for the direction-aware defaults and never the merged default rules.

Draft 2020-12 is the only supported dialect. These inputs return an error:

  • a $schema that names another dialect
  • a $dynamicRef that needs dynamic scope resolution
  • a schema without $schema that uses a keyword from an earlier draft, such as definitions or dependencies
  • YAML anchors, aliases, and keys that are not strings

Generating TypeScript models

generator/typescript renders the component schemas of an OpenAPI document as a type-only TypeScript module, built from the same model as the Go generator.

file, err := typescript.RenderSchemas(docModel.Model.Components.Schemas)
os.WriteFile("api.gen.ts", file.Source, 0o644)

See generator/typescript/README.md for how each schema shape maps to TypeScript.


Quick-start tutorial

👀 Get rolling fast using libopenapi with the Parsing OpenAPI files using go guide 👀

Or, follow these steps and see something in a few seconds.

Step 1: Grab the petstore

curl https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/_archive_/schemas/v3.0/pass/petstore.yaml > petstorev3.json

Step 2: Grab libopenapi

go get github.com/pb33f/libopenapi

Step 3: Parse the petstore using libopenapi

Copy and paste this code into a main.go file.

package main

import (
	"fmt"
	"os"
	"github.com/pb33f/libopenapi"
)

func main() {
	petstore, _ := os.ReadFile("petstorev3.json")
	document, err := libopenapi.NewDocument(petstore)
	if err != nil {
		panic(fmt.Sprintf("cannot create new document: %e", err))
	}
	docModel, err := document.BuildV3Model()
	if err != nil {
		panic(fmt.Sprintf("cannot create v3 model from document: %e", err))
	}

	// The following fails after the first iteration
	for schemaName, schema := range docModel.Model.Components.Schemas.FromOldest() {
		if schema.Schema().Properties != nil {
			fmt.Printf("Schema '%s' has %d properties\n", schemaName, schema.Schema().Properties.Len())
		}
	}
}

Run it, which should print out:

Schema 'Pet' has 3 properties
Schema 'Error' has 2 properties

Read the full docs at https://pb33f.io/libopenapi/


Logo gopher is modified, originally from egonelbre

About

libopenapi is a fully featured, high performance OpenAPI 3.2, 3.1, 3.0, Overlays and Arazzo parser, library, validator and toolkit for go applications.

Topics

Resources

Security policy

Stars

891 stars

Watchers

6 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages