The API

Everything these packages expose falls into five groups: the generated types themselves, the encode/decode pair that moves them to and from JSON, the model-type registry that turns a type name in a document into a Julia type, the unit annotations that say what a number means, and the two document containers that hold a whole system or portfolio.

The surface is deliberately small. There is no client, no HTTP, and no mutation API: these are transport types, and the code that builds a System out of them lives in PowerSystems.jl.

Generated types

Every schema becomes one Julia struct, keyword-constructible, with a field per property:

using PowerOpenAPIModels, JSON

bus = ACBus(;
    id = 1,
    number = 1,
    name = "bus1",
    available = true,
    bustype = ACBusType("REF"),
    base_voltage = 138.0,
)
ACBus(1, 1, "bus1", true, ACBusType("REF"), ABSENT, ABSENT, ABSENT, ABSENT, 138.0, ABSENT, ABSENT, Dict{String, Any}())

Three shapes exist, distinguished by their supertype:

SupertypeSchema shapeExample
APIModelan objectACBus, Line, MinMax
EnumAPIModela string with a fixed value setACBusType, UnitSystem
OneOfAPIModela oneOf selected by a discriminatorValueCurve, TimeSeriesAssociation

Every generated struct is immutable. Build a modified copy rather than assigning to a field.

Required and optional fields

A required property becomes a plain typed field with no default: leave it out and the constructor errors. An optional property defaults to ABSENT, a sentinel distinct from nothing:

  • ABSENT — the key is not in the document at all. It encodes to no key.
  • nothing — the key is in the document with the value null, where the schema allows it.
line_without_b = !haskey(encode(ACBus(; id = 1, number = 1, name = "b", available = true)), "angle")
true

That distinction is what lets a document round-trip without gaining keys its producer never wrote.

Unknown properties

Every object type carries an additional_properties dictionary. A key the schema does not model is kept there rather than dropped, so a document written by a newer producer survives a read-then-write cycle intact:

decoded = decode(MinMax, Dict{String, Any}("min" => 0.0, "note" => "from a newer producer"))
decoded.additional_properties
Dict{String, Any} with 1 entry:
  "note" => "from a newer producer"

Encoding and decoding

Two functions — encode and decode — both exported by InfrastructureCoreOpenAPIModels and therefore by every package that depends on it.

encode returns JSON-ready data — an object, not a string — so it composes with whatever JSON library the caller already uses:

text = JSON.json(encode(bus))
"{\"id\":1,\"number\":1,\"name\":\"bus1\",\"available\":true,\"bustype\":\"REF\",\"base_voltage\":138.0}"
back = decode(ACBus, JSON.parse(text))
back.base_voltage
138.0

Decoding validates against the schema each package embeds, so malformed input fails at the boundary rather than deep inside a consumer:

decode(Line, Dict("id" => 1))
# ERROR: schema validation failed while decoding Line
#   : required

Pass false as the third argument to skip validation when the data is already known good and the check is measurably hot.

Enums

An enum is a wrapper struct that validates on construction, so an invalid value cannot exist:

string(ACBusType("REF"))
"REF"
ACBusType("SWING")   # ERROR: ArgumentError: invalid ACBusType value "SWING"

string unwraps one; the raw value is in the value field.

Discriminated unions

A oneOf becomes a wrapper struct holding the selected variant in value. On decode the discriminator property picks the variant; on encode the variant is written directly, with no wrapper object around it:

curve = ValueCurve(
    InputOutputCurve(;
        function_data = InputOutputCurveFunctionData(
            LinearFunctionData(; constant_term = 0.0, proportional_term = 25.0),
        ),
    ),
)
decode(ValueCurve, JSON.parse(JSON.json(encode(curve)))).value
InputOutputCurve("INPUT_OUTPUT", InputOutputCurveFunctionData(LinearFunctionData(0.0, "LINEAR", 25.0, Dict{String, Any}())), ABSENT, Dict{String, Any}())

Dispatch on OneOfAPIModel to unwrap one generically instead of enumerating members.

The model-type registry

A document names its component types as strings. The registry is what turns a name back into a type, and every package registers its own types when it loads — so what a build can read depends on which packages are loaded, and asking for an unknown name is an error, never a silent skip.

InfrastructureCoreOpenAPIModels.model_type("ACBus")
ACBus

Reference: register_model_type!, has_model_type, model_type, DocumentFormatError.

Units

Every convertible field carries its unit from the schema's x-unit annotation, reachable without parsing anything. Three cases:

Fixed — the unit follows from the type:

declared_unit(ACBus, Val(:base_voltage)), declared_quantity(ACBus, Val(:base_voltage))
("kV", "Voltage")

Discriminated — the unit follows from a basis the record carries itself, so it is read from the instance:

zone(units) = LoadZone(;
    id = 1,
    name = "zone1",
    peak_active_power = 100.0,
    peak_reactive_power = 50.0,
    base_power = 100.0,
    power_units = UnitSystem(units),
)

(
    declared_unit(zone("NATURAL_UNITS"), Val(:peak_active_power)),
    declared_unit(zone("COMPONENT_BASE"), Val(:peak_active_power)),
)
("MW", "pu")

There is no system-wide unit basis: data per-unitized against a shared base records that base in the component's own base_power and travels as COMPONENT_BASE.

Per-unit — the value needs a base, and unit_base names the sibling field holding it:

unit_base(ACBus, Val(:magnitude))
:base_voltage

Unannotated fields fall through to a generic false, so a caller can ask about any field:

has_declared_unit(ACBus, Val(:name))
false

The vocabulary converts a unit to its quantity's canonical unit. It is keyed by the pair, because a unit is only meaningful for a quantity, and pu is deliberately absent from it — a per-unit value needs a base, not a factor:

(
    conversion_factor("ActivePower", "kW"),
    conversion_factor("Angle", "deg"),
    has_conversion_factor("Voltage", "pu"),
)
(0.001, 0.017453292519943295, false)

Documents

Two containers hold a complete data set: SystemDocument in PowerCoreOpenAPIModels and PortfolioDocument in PowerInvestmentsOpenAPIModels. Both subtype AbstractDocument, and the operations they share — ids, components, supplemental attributes, ext, writing, and schema versioning — live in InfrastructureCoreOpenAPIModels, imported into both so either module reaches them. They are hand-written rather than generated, because typed heterogeneous components buckets are not expressible in OpenAPI — so their fields are asserted against the schemas in the test suite instead.

A document resolves its component and association types by name through the model-type registry, so a reader needs the domain packages that define those types loaded — not the umbrella.

Their accessors are not exported: reach them qualified, so get_name here never collides with a consumer's own.

using PowerCoreOpenAPIModels
const P = PowerCoreOpenAPIModels

doc = P.SystemDocument(; name = "example", frequency = 60.0)

bus_id = P.next_id!(doc)
P.add_component!(doc, ACBus(;
    id = bus_id,
    number = 1,
    name = "bus1",
    available = true,
    bustype = ACBusType("REF"),
))
P.set_ext!(doc, bus_id, Dict("Zone" => "1"))

path = joinpath(mktempdir(), "system.json")
P.write_document(doc, path)
back = P.read_document(path)

(P.get_name(back), P.component_type_names(back), eltype(P.get_components(back, "ACBus")))
("example", ["ACBus"], ACBus)

Ids are the document's reference mechanism, so next_id! is the only way to mint one, and reading a document reserves every id it already contains. read_document runs validate_document: a duplicate id or an association pointing at a component that is not there raises DocumentFormatError instead of being read and quietly dropped.

Every document is stamped with a schema_version, checked before anything is decoded: a document that is missing it, malformed, from another compatibility line, or newer than the package raises SchemaVersionError. check_schema_version classifies raw JSON without throwing. write_document stamps the package's version by default (schema_version = :current); schema_version = :source stamps the version the document was read at (get_source_schema_version), validated against that version's strict bundle and needing JSONSchema.jl loaded. upgrade_document rewrites an older document on the same line with the current stamp.

PortfolioDocument is the investment-side counterpart with the same shape, read back with PowerInvestmentsOpenAPIModels.read_portfolio_document.

Reference: AbstractDocument, SystemDocument, PortfolioDocument, next_id!, add_component!, add_supplemental_attribute!, add_time_series_association!, get_components, component_type_names, set_ext!, get_ext, validate_document, write_document, read_document, read_portfolio_document, upgrade_portfolio_document.

Choosing a package

Depend on the narrowest package that covers the types in play. PowerOpenAPIModels pulls in all six; a parser that only emits operations components needs PowerOperationsOpenAPIModels, and a consumer of time series catalogs needs only InfrastructureTimeSeriesOpenAPIModels. Each package re-exports the ones it depends on, so narrowing costs no using lines.