Documentation
¶
Overview ¶
Package schemautil provides utilities for working with OpenAPI schema types.
This package centralizes type assertion patterns for OAS version-specific fields, particularly handling the differences between OAS 2.0/3.0 (string types) and OAS 3.1+ (array types for nullable support).
Index ¶
- func GetPrimaryType(schema *parser.Schema) string
- func GetSchemaTypes(schema *parser.Schema) []string
- func HasType(schema *parser.Schema, targetType string) bool
- func IsNullable(schema *parser.Schema) bool
- func IsSingleType(schema *parser.Schema) bool
- type CompareFunc
- type DeduplicationConfig
- type DeduplicationResult
- type SchemaDeduplicator
- type SchemaHasher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetPrimaryType ¶
GetPrimaryType returns the first non-null type from a schema. This is useful for OAS 3.1+ where type arrays may include "null".
Returns an empty string if the schema is nil or has no types.
func GetSchemaTypes ¶
GetSchemaTypes returns the type(s) from a schema, handling both string (OAS 2.0/3.0) and []any (OAS 3.1+) representations.
Examples:
- OAS 3.0: {"type": "string"} returns ["string"]
- OAS 3.1: {"type": ["string", "null"]} returns ["string", "null"]
func IsNullable ¶
IsNullable checks if the schema allows null values. In OAS 3.1+, this is indicated by "null" in the type array. In OAS 3.0, this is indicated by the nullable field (not checked here).
func IsSingleType ¶
IsSingleType returns true if the schema has exactly one type (not counting null).
Types ¶
type CompareFunc ¶ added in v1.26.0
CompareFunc compares two schemas for structural equivalence. Returns true if the schemas are semantically identical. This function type allows dependency injection to avoid import cycles.
type DeduplicationConfig ¶ added in v1.26.0
type DeduplicationConfig struct {
// EquivalenceMode controls comparison depth ("deep" recommended).
// Uses joiner.EquivalenceMode values: "none", "shallow", "deep".
EquivalenceMode string
}
DeduplicationConfig configures semantic schema deduplication behavior.
func DefaultDeduplicationConfig ¶ added in v1.26.0
func DefaultDeduplicationConfig() DeduplicationConfig
DefaultDeduplicationConfig returns a DeduplicationConfig with sensible defaults.
type DeduplicationResult ¶ added in v1.26.0
type DeduplicationResult struct {
// CanonicalSchemas maps canonical names to their schema definitions.
// Only canonical schemas are included; duplicates are removed.
CanonicalSchemas map[string]*parser.Schema
// Aliases maps alias schema names to their canonical name.
// All references to alias names should be rewritten to canonical names.
Aliases map[string]string
// RemovedCount is the number of duplicate schemas that were removed.
RemovedCount int
// EquivalenceGroups maps canonical names to all equivalent schema names.
// Includes the canonical name itself as the first element.
EquivalenceGroups map[string][]string
}
DeduplicationResult contains the outcome of schema deduplication.
func (*DeduplicationResult) CanonicalName ¶ added in v1.26.0
func (r *DeduplicationResult) CanonicalName(name string) string
CanonicalName returns the canonical name for a schema name. If the name is not an alias, it returns the name unchanged.
func (*DeduplicationResult) IsAlias ¶ added in v1.26.0
func (r *DeduplicationResult) IsAlias(name string) bool
IsAlias returns true if the given name is an alias (not canonical).
func (*DeduplicationResult) IsCanonical ¶ added in v1.26.0
func (r *DeduplicationResult) IsCanonical(name string) bool
IsCanonical returns true if the given name is a canonical schema name.
type SchemaDeduplicator ¶ added in v1.26.0
type SchemaDeduplicator struct {
// contains filtered or unexported fields
}
SchemaDeduplicator identifies and consolidates semantically identical schemas.
func NewSchemaDeduplicator ¶ added in v1.26.0
func NewSchemaDeduplicator(config DeduplicationConfig, compare CompareFunc) *SchemaDeduplicator
NewSchemaDeduplicator creates a new SchemaDeduplicator. The compare function is used to verify equivalence after hash grouping. If compare is nil, schemas are considered equivalent if they have the same hash (not recommended due to potential hash collisions).
func (*SchemaDeduplicator) Deduplicate ¶ added in v1.26.0
func (d *SchemaDeduplicator) Deduplicate(schemas map[string]*parser.Schema) (*DeduplicationResult, error)
Deduplicate identifies semantically identical schemas and consolidates them. It returns a result containing only canonical schemas and a mapping of aliases.
The algorithm:
- Group schemas by structural hash (O(N))
- Verify equivalence within each group using deep comparison
- Select canonical name (alphabetically first) for each equivalence group
- Build alias mapping and return only canonical schemas
type SchemaHasher ¶ added in v1.26.0
type SchemaHasher struct {
// contains filtered or unexported fields
}
SchemaHasher computes structural hashes for schemas. Structural hashes ignore metadata fields (title, description, example, deprecated) and focus on fields that affect the schema's semantic meaning.
func NewSchemaHasher ¶ added in v1.26.0
func NewSchemaHasher() *SchemaHasher
NewSchemaHasher creates a new SchemaHasher.
func (*SchemaHasher) GroupByHash ¶ added in v1.26.0
GroupByHash groups schemas by their structural hash. Returns a map from hash value to list of schema names with that hash.