openapi

package
v0.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 15 Imported by: 1

Documentation

Index

Constants

View Source
const SchemaCreate = "Create"
View Source
const SchemaIDOnlyName = "Schema.IDOnly"

Variables

View Source
var IDOnlySchema = ogen.NewSchema().AddRequiredProperties(
	PrimitiveToOgenTypeMaps[reflect.Uint64]().ToProperty("id"),
)
View Source
var PrimitiveToOgenTypeMaps = map[reflect.Kind]func() *ogen.Schema{
	reflect.Bool:  ogen.Bool,
	reflect.Int:   ogen.Int,
	reflect.Int8:  ogen.Int32,
	reflect.Int16: ogen.Int32,
	reflect.Int32: ogen.Int32,
	reflect.Int64: ogen.Int64,
	reflect.Uint: func() *ogen.Schema {
		return ogen.Int().SetMinimum(&UintMinValue)
	},
	reflect.Uint8: func() *ogen.Schema {
		return ogen.Int().SetMinimum(&UintMinValue)
	},
	reflect.Uint16: func() *ogen.Schema {
		return ogen.Int().SetMinimum(&UintMinValue)
	},
	reflect.Uint32: func() *ogen.Schema {
		return ogen.Int32().SetMinimum(&UintMinValue)
	},
	reflect.Uint64: func() *ogen.Schema {
		return ogen.Int64().SetMinimum(&UintMinValue)
	},
	reflect.Uintptr: func() *ogen.Schema {
		return ogen.Int64().SetMinimum(&UintMinValue)
	},
	reflect.Float32:    ogen.Float,
	reflect.Float64:    ogen.Double,
	reflect.Complex64:  ogen.Float,
	reflect.Complex128: ogen.Float,
	reflect.String:     ogen.String,
}

PrimitiveToOgenTypeMaps is a map that maps Go primitive types to their corresponding Ogen schema types.

View Source
var UintMinValue int64 = 0

Functions

func ContentCreateSchema

func ContentCreateSchema(s *schema.Schema) *ogen.Schema

ContentCreateSchema creates an OpenAPI schema for creating content.

func ContentDetailSchema

func ContentDetailSchema(s *schema.Schema) *ogen.Schema

ContentDetailSchema returns the OpenAPI schema for the content detail.

func ContentListResponseSchema

func ContentListResponseSchema(s *schema.Schema) *ogen.Schema

ContentListResponseSchema generates the OpenAPI schema for the content list response.

func CreateInfoObject

func CreateInfoObject() ogen.Info

func CreateParameters

func CreateParameters(args fs.Args, params []string) ([]*ogen.Parameter, error)

CreateParameters creates openapi parameters from app args.

func CreateParamsObject

func CreateParamsObject() map[string]*ogen.Parameter

func ExtractPathParams

func ExtractPathParams(s string) []string

ExtractPathParams extracts path parameters from a given string.

It uses a regular expression to find all occurrences of ":param" pattern
and returns a slice of the captured parameter names.

func GetSchemaRefName

func GetSchemaRefName(s string, types ...string) string

GetSchemaRefName returns the reference name for a schema with a custom type.

If no types are provided, it returns the reference name in the format "Schema.<s capitalized>".
If types are provided, it returns the reference name in the format "Schema.<s capitalized>.<types[0]>".

func JoinPaths

func JoinPaths(paths ...string) string

JoinPaths concatenates multiple path segments into a single path string.

It removes any duplicate slashes in the resulting path.

func MergeArgs

func MergeArgs(first fs.Args, second fs.Args) (fs.Args, error)

MergeArgs merges two app.Args maps and returns a new app.Args map.

If there are duplicate keys in the second map, an error is returned.

func MergeParameters

func MergeParameters(first []*ogen.Parameter, second []*ogen.Parameter) []*ogen.Parameter

MergeParameters merges two slices of parameters.

func MergePathItems

func MergePathItems(first map[string]*ogen.PathItem, second map[string]*ogen.PathItem) map[string]*ogen.PathItem

MergePathItems merges two maps of path items into a single map.

It takes two maps of type map[string]*ogen.PathItem as input and returns a new map of type map[string]*ogen.PathItem.
The function iterates over the second map and checks if each key exists in the first map.
If the key exists, it merges the corresponding path item properties from the second map into the first map.
If the key does not exist, it adds the key-value pair from the second map to the first map.
The resulting map contains all the path items from both input maps, with any overlapping keys merged.

func NormalizePath

func NormalizePath(path string) (string, []string)

NormalizePath normalizes the given path by converting it into the OpenAPI format.

It replaces any path parameters in the form of ":param" with "{param}".
The function returns the normalized path, a slice of extracted path parameters, and any error encountered.

func RefSchema

func RefSchema(name string) *ogen.Schema

RefSchema returns a new instance of ogen.Schema with the given name as the reference.

The reference is constructed using the provided name and the "#/components/schemas/" prefix.

Types

type OpenAPISpec

type OpenAPISpec struct {
	// contains filtered or unexported fields
}

func NewSpec

func NewSpec(config *OpenAPISpecConfig) (*OpenAPISpec, error)

NewSpec creates a new OpenAPISpec instance based on system resources and schema builder.

func (*OpenAPISpec) Create

func (oas *OpenAPISpec) Create() (err error)

func (*OpenAPISpec) CreateMapSchema

func (oas *OpenAPISpec) CreateMapSchema(dtType reflect.Type) *ogen.Schema

CreateMapSchema creates a schema for a map data type in the OpenAPI specification.

If the data type is map[string]interface{}, return a schema for map of any type.
Otherwise, it retrieves the element schema for the data type using the GetElemSchema method.
If the element schema is not found, it a schema for map of any type.

func (*OpenAPISpec) CreatePathItem

func (oas *OpenAPISpec) CreatePathItem(r *ResourceInfo) (map[string]*ogen.PathItem, error)

CreatePathItem creates a PathItem object for the given ResourceInfo.

Returns a map of path strings to PathItem objects.

func (*OpenAPISpec) CreateResourcesForSchemas

func (oas *OpenAPISpec) CreateResourcesForSchemas()

CreateResourcesForSchemas creates resources for the system schemas.

func (*OpenAPISpec) CreateSliceSchema

func (oas *OpenAPISpec) CreateSliceSchema(dtType reflect.Type) *ogen.Schema

CreateSliceSchema creates a schema for a slice type in the OpenAPI specification.

If the input type is []interface{}, it creates a schema for slice of any type.
Otherwise, it creates a schema for a slice with a specific element type.
The function sets the maximum number of items in the schema if the input type is an array.
If the element schema cannot be retrieved, it returns nil.

func (*OpenAPISpec) CreateStructSchema

func (oas *OpenAPISpec) CreateStructSchema(dtType reflect.Type, fieldNames ...string) *ogen.Schema

CreateStructSchema creates a schema for a struct data type in the OpenAPI specification.

Struct may contain nested struct, so we need to use the reference schema.
The reference schemas are stored in OpenAPISpec.referenceSchemas and will be created later.

func (*OpenAPISpec) GetElemSchema

func (oas *OpenAPISpec) GetElemSchema(rt reflect.Type) *ogen.Schema

GetElemSchema returns the OpenAPI schema for the element type slice or map.

func (*OpenAPISpec) GetSchemaIndex

func (oas *OpenAPISpec) GetSchemaIndex() int

func (*OpenAPISpec) ResolveSchemaReferences

func (oas *OpenAPISpec) ResolveSchemaReferences()

ResolveSchemaReferences resolves the schema references in the OpenAPISpec.

Schema may reference other schemas or itself.
When creating schema, if a field is a struct/map, we create a reference schema for it.
These references need to be created so openapi can resolve them.
While creating reference schema, there may be new reference schemas created.
We need to resolve these new reference schemas as well.

func (*OpenAPISpec) Schema

func (oas *OpenAPISpec) Schema(name string) *ogen.Schema

func (*OpenAPISpec) SchemaToOGenSchema

func (oas *OpenAPISpec) SchemaToOGenSchema(s *schema.Schema)

SchemaToOGenSchema converts a schema.Schema object to an ogen.Schema object and adds it to the OpenAPISpec.

func (*OpenAPISpec) SchemasToOGenSchemas

func (oas *OpenAPISpec) SchemasToOGenSchemas()

SchemasToOGenSchemas converts the schemas defined in the OpenAPISpec to OGenSchemas.

It iterates over the schema builder's schemas and calls schemaToOGenSchema for each schema.

func (*OpenAPISpec) Spec

func (oas *OpenAPISpec) Spec() []byte

func (*OpenAPISpec) TypeToOgenSchema

func (oas *OpenAPISpec) TypeToOgenSchema(t any, configs ...*TypeToOgenConfig) (structSchema *ogen.Schema)

TypeToOgenSchema converts a given type to an ogen.Schema object based on the OpenAPI specification.

It accepts a type `t` and optional configuration parameters `configs`.
If the type `t` is already an ogen.Schema object, it is returned as is.
If the type `t` is not dereferenceable, an empty ogen.Schema object is returned.
For primitive types, the corresponding ogen.Schema object is created and returned.
For the `time.Time` type, an ogen.Schema object representing a DateTime is returned.
For the `[]uint8` type, an ogen.Schema object representing Bytes is returned.
For slice or array types, the corresponding ogen.Schema object is created and returned.
For map types, the corresponding ogen.Schema object is created and returned.
For struct types, the corresponding ogen.Schema object is created and returned, unless the `skipStruct` configuration is set to true.
If none of the above conditions are met, nil is returned.

type OpenAPISpecConfig

type OpenAPISpecConfig struct {
	Resources     *fs.ResourcesManager
	SchemaBuilder *schema.Builder
	BaseURL       string
}

type ReferenceSchemaType

type ReferenceSchemaType struct {
	Name string
	Type reflect.Type
}

type ResourceInfo

type ResourceInfo struct {
	ID         string
	Path       string
	Method     string
	Signatures fs.Signatures
	Args       fs.Args
	Public     bool
}

func FlattenResources

func FlattenResources(resources []*fs.Resource, prefix string, args fs.Args) ([]*ResourceInfo, error)

FlattenResources takes a slice of resources, a prefix string, and app.Args as input,

and returns a flattened list of ResourceInfo structs and an error.
It recursively flattens the resources and generates a path for each method in the resource.
If a resource is a group, it appends the group prefix to the path.
If a resource has no method, it uses "GET" as the default method.
The ResourceInfo struct contains information about the resource, such as ID, signature, method, path, args, and public status.
If there is an error during the flattening process, it returns nil and the error.

func (*ResourceInfo) Clone

func (r *ResourceInfo) Clone() *ResourceInfo

Clone creates a copy of the ResourceInfo object.

type TypeToOgenConfig

type TypeToOgenConfig struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL