schemautil

package
v1.45.2 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPrimaryType

func GetPrimaryType(schema *parser.Schema) string

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

func GetSchemaTypes(schema *parser.Schema) []string

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 HasType

func HasType(schema *parser.Schema, targetType string) bool

HasType checks if the schema includes the specified type.

func IsNullable

func IsNullable(schema *parser.Schema) bool

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

func IsSingleType(schema *parser.Schema) bool

IsSingleType returns true if the schema has exactly one type (not counting null).

Types

type CompareFunc added in v1.26.0

type CompareFunc func(left, right *parser.Schema) bool

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:

  1. Group schemas by structural hash (O(N))
  2. Verify equivalence within each group using deep comparison
  3. Select canonical name (alphabetically first) for each equivalence group
  4. 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

func (h *SchemaHasher) GroupByHash(schemas map[string]*parser.Schema) map[uint64][]string

GroupByHash groups schemas by their structural hash. Returns a map from hash value to list of schema names with that hash.

func (*SchemaHasher) Hash added in v1.26.0

func (h *SchemaHasher) Hash(schema *parser.Schema) uint64

Hash computes a structural hash for a schema. Schemas with identical structural properties will have the same hash. Note: Hash collisions are possible; use deep comparison to verify equivalence.

Jump to

Keyboard shortcuts

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