flutter_api_contract library

Detect drift between Dart models and an OpenAPI/Swagger schema.

Classes

ApiSpec
A parsed API description.
Baseline
Known drift that is accepted for now. It works like a ratchet: entries can only be removed (--update-baseline never adds to an existing file).
BaselineResult
Codes
Stable finding codes (also used in baselines and --format json).
ContractChecker
Compares every matched model with its schema.
ContractConfig
Settings from api_contract.yaml. Every field is optional.
DartEnum
An enum used by models.
DartModel
A class that is deserialized from JSON.
DartProject
Everything extracted from a Dart project.
DartType
A Dart type as written in source, e.g. List<Pet>?.
ExitCodes
Exit codes.
FieldRead
One JSON key a model reads.
Finding
One drift between a Dart model and the API schema.
MatchResult
Result of pairing every model with a schema.
ModelMatch
A model paired with the schema it is checked against.
Report
Everything a reporter needs.
SchemaNode
One schema node. $refs are kept as names and resolved lazily through ApiSpec.resolve, so recursive schemas (a Category with a parent Category) are fine.

Enums

FieldRename
json_serializable's FieldRename, applied the same way the generator does it.
JsonType
The JSON type a schema node accepts.
MatchVia
How a model was paired with a schema.
ModelKind
How a model is deserialized.
ReportFormat
Output formats for --format.
Severity
How bad a finding is. Only error fails the run by default.

Constants

packageVersion → const String
Package version, printed by --version.

Functions

closest(String key, Iterable<String> candidates) → String?
The candidate closest to key, or null when nothing is close enough.
extractFromSources(Map<String, String> sources, {FieldRename globalRename = FieldRename.none}) → DartProject
Extracts models from in-memory sources keyed by relative path.
extractProject(String root, {List<String> paths = const ['lib'], List<String> exclude = const []}) → DartProject
Scans root for Dart models and enums.
levenshtein(String a, String b) → int
Edit distance between a and b.
loadSpec(String source, {Map<String, String> headers = const {}, Duration timeout = const Duration(seconds: 30)}) → Future<ApiSpec>
Loads an OpenAPI/Swagger document from a local path or an http(s) URL.
matchModels(DartProject project, ApiSpec spec, ContractConfig config) → MatchResult
Pairs models with schemas: explicit models: mapping, then endpoints:, then exact name, then name with suffixes stripped.
parseSchema(Object? raw) → SchemaNode
Parses one raw schema object (no $ref following).
parseSpecText(String text, {String source = '<spec>'}) → ApiSpec
Parses spec text (JSON or YAML) into an ApiSpec.
readGlobalFieldRename(String root) → FieldRename?
Reads field_rename from the json_serializable builder options in <root>/build.yaml.
readSpecText(String source, {Map<String, String> headers = const {}, Duration timeout = const Duration(seconds: 30)}) → Future<String>
Reads the raw text of a spec from a file or URL.
refName(String ref) → String
#/components/schemas/Pet -> Pet.
runCli(List<String> arguments, {StringSink? out, StringSink? err}) → Future<int>
Runs the CLI and returns the exit code.
yamlToPlain(Object? node) → Object?
Converts package:yaml nodes into plain maps/lists/scalars.

Exceptions / Errors

ConfigException
Thrown for an invalid api_contract.yaml.
SpecException
Thrown when the spec cannot be fetched or parsed.