Extending API DataModel
The Ed-Fi Alliance provides a tool called MetaEd IDE that
allows you to author and build extensions using a simple language. Building
your extension generates an ApiSchema-EXTENSION.json file describing the new
or extended resources, and the Ed-Fi API reads it directly, with no code
generation or recompilation of the API itself required.
For a complete, hands-on walkthrough, see How To: Extend the Ed-Fi API - Alternative Education Program Example. For a more advanced scenario, see How To: Extend the Ed-Fi API - Student Transcript Example.
The details below provide additional implementation information for developers.
The ApiSchema Extension Model
The Ed-Fi API loads its data model from a core ApiSchema.json file (the
Ed-Fi Data Standard, with isExtensionProject: false) plus, optionally, one or
more extension ApiSchema-EXTENSION.json files produced by MetaEd (with
isExtensionProject: true). Together these make up the effective API
schema (see Database Provisioning).
An extension schema can:
- Add wholly new resources and descriptors (as in the Alternative Education Program example).
- Extend an existing core resource with additional properties. MetaEd marks
these as
isResourceExtensionin the schema, and the added properties are nested under an_extnode on the core resource's JSON representation rather than replacing it.
Workflow
- Author the extension in MetaEd, targeting the data model version used
by the Ed-Fi API (e.g.
ed-fi-model-5.2). See MetaEd IDE - Creating and Maintaining Your Extension for how to set up the extension project and author entities. - Build the project to generate
ApiSchema-EXTENSION.json. - Stage the schema workspace. Put the core and extension schemas in one
directory, together with a
bootstrap-api-schema-manifest.jsonfile that lists them. Then setAppSettings:ApiSchema:UseCustomPathtotrueandAppSettings:ApiSchema:Pathto that directory (see Configuration Details). Choose one way to build the directory:- In a local environment, put the core
ApiSchema.jsonand yourApiSchema-EXTENSION.jsonin one folder and runprepare-dms-schema.ps1 -ApiSchemaPath <folder>. Without-ApiSchemaPath, the script stages only the Ed-Fi packages listed inSCHEMA_PACKAGES, not your extension. - In the Ed-Fi API container, if you load only Ed-Fi-published packages (such
as the Data Standard and TPDM), list them in
SCHEMA_PACKAGES. The container downloads them and writes the manifest at startup; see Loading Schemas from Packages. - To load your own extension anywhere else, write the manifest yourself; see The Schema Workspace Manifest.
- In a local environment, put the core
- Provision a new database. Run
api-schema-tools ddl provisionwith the core and extension schemas (in a local environment,bootstrap-local-dms.ps1does this). Use exactly the schema files the Ed-Fi API will load (withSCHEMA_PACKAGES, the same package names and versions). The Ed-Fi API won't use a database that was provisioned for a different set of schemas. See Database Provisioning. - Restart the Ed-Fi API. The schema is loaded once at startup; there is no hot reload. Any later change to an extension schema requires repeating steps 3 and 4, then restarting the service again.
- Author security metadata. Once the stack is back up, new resources are still not accessible until they are added to the claims hierarchy and granted to a claim set through the running Configuration Service. See How To: Create and Manage API Security Metadata.
The Schema Workspace Manifest
You need this section only if you stage the schema files yourself;
prepare-dms-schema.ps1 and the container's SCHEMA_PACKAGES option write the
manifest for you.
When AppSettings:ApiSchema:UseCustomPath is true, the Ed-Fi API reads
bootstrap-api-schema-manifest.json from the root of
AppSettings:ApiSchema:Path and loads only the schema files it lists. Other
files in the directory are ignored. If the directory, the manifest, or a listed
schema file is missing or invalid, the Ed-Fi API logs the problem and doesn't
start.
This example manifest stages the core Ed-Fi Data Standard schema and the Alternative Education Program example extension:
{
"version": 1,
"projects": [
{
"projectName": "Ed-Fi",
"projectEndpointName": "ed-fi",
"isExtensionProject": false,
"schemaPath": "schemas/Ed-Fi/ApiSchema.json",
"discoverySpecPath": "schemas/Ed-Fi/discovery-spec.json"
},
{
"projectName": "SampleAlternativeEducationProgram",
"projectEndpointName": "sample-alternative-education-program",
"isExtensionProject": true,
"schemaPath": "schemas/SampleAlternativeEducationProgram/ApiSchema.json"
}
]
}
The Ed-Fi API checks the manifest at startup:
versionmust be1.projectsmust list exactly one core project (isExtensionProject: false) and any number of extension projects.- Each project needs
projectName,projectEndpointName,isExtensionProject, andschemaPath. The first three must match theprojectSchemavalues in the schema file exactly. schemaPathis relative to the manifest's directory. Absolute paths and paths that leave the directory (..) are rejected. The schema file can have any name.
Two more fields use the same path rules:
discoverySpecPath(core project): the discovery specification file that the Ed-Fi API serves at/metadata/specifications/discovery-spec.json. The manifest is valid without it, but that endpoint then returns an error, so include it. The Ed-Fi Data Standard ApiSchema package ships this file asdiscovery-spec.json.xsdDirectory(any project, optional): a directory of XSD files that the Ed-Fi API serves from/metadata/xsd. The Bulk Load Client needs these to load extension XML.
Loading Schemas from Packages
The Ed-Fi API container can download the schema files from NuGet packages
instead of reading files you stage. Set SCHEMA_PACKAGES to a JSON array with
one entry per package. Each entry has three fields:
name: the package ID.version: the package version.feedUrl: the NuGet v3 feed that hosts the package.
List the core Data Standard package along with any extension packages. This example loads the Data Standard 5.2 core schema and the TPDM extension from the Ed-Fi package feed:
USE_API_SCHEMA_PATH=true
API_SCHEMA_PATH=/app/ApiSchema
SCHEMA_PACKAGES='[{"name":"EdFi.DataStandard52.ApiSchema","version":"1.0.335","feedUrl":"https://pkgs.dev.azure.com/ed-fi-alliance/Ed-Fi-Alliance-OSS/_packaging/EdFi%40Release/nuget/v3/index.json"},{"name":"EdFi.DataStandard52.TPDM.ApiSchema","version":"1.0.335","feedUrl":"https://pkgs.dev.azure.com/ed-fi-alliance/Ed-Fi-Alliance-OSS/_packaging/EdFi%40Release/nuget/v3/index.json"}]'
Keep the SCHEMA_PACKAGES value on a single line, as in the example.
The container reads SCHEMA_PACKAGES only when the
AppSettings__ApiSchema__UseCustomPath environment variable is true
(lowercase), and it doesn't start unless AppSettings__ApiSchema__Path is also
set. Setting these values in appsettings.json instead isn't enough: the
download is skipped, and the Ed-Fi API then finds no manifest and doesn't
start. In Docker Compose, set USE_API_SCHEMA_PATH and API_SCHEMA_PATH, as
in the example. At startup, the container:
- Deletes the
PackagesandDownloadedPackagesdirectories underAppSettings:ApiSchema:Path. - Downloads each listed package into that directory.
- Writes
bootstrap-api-schema-manifest.jsonfrom the packages' own manifests, replacing any manifest already there.
Each package must contain a package-manifest.json file with version set to
1 and the same project fields as a schema workspace
manifest entry. If a package doesn't have one,
the container doesn't start. The Ed-Fi ApiSchema packages include this file.
MetaEd's build output for your own extension is an ApiSchema-EXTENSION.json
file, not a package. To load it, stage it with prepare-dms-schema.ps1 or write
the manifest yourself.
Naming Conventions for Extensions
MetaEd generates ApiSchema-EXTENSION.json from what you configure in
your extension project (the projectName in package.json) and what you author
in your .metaed source files (entity names, Descriptor declarations, and so
on). The excerpt below shows what that generated file looks like for the
Alternative Education Program
example:
{
"apiSchemaVersion": "1.0.0",
"projectSchema": {
"projectName": "SampleAlternativeEducationProgram",
"projectEndpointName": "sample-alternative-education-program",
"projectVersion": "1.0.0",
"isExtensionProject": true,
"resourceSchemas": {
"studentAlternativeEducationProgramAssociations": {
"resourceName": "StudentAlternativeEducationProgramAssociation",
"isDescriptor": false,
"isResourceExtension": false
},
"alternativeEducationEligibilityReasonDescriptors": {
"resourceName": "AlternativeEducationEligibilityReasonDescriptor",
"isDescriptor": true,
"isResourceExtension": false
}
}
}
}
| Field | Where it comes from |
|---|---|
isExtensionProject | Always true for an extension project. MetaEd sets this automatically to distinguish your project from the core Ed-Fi Data Standard project (false); you don't configure it directly. |
projectEndpointName | Derived by MetaEd from the projectName you set in your extension's package.json (step 1 above), hyphenated and lower-cased. |
resourceName | Taken from the entity name you author in your .metaed source file (e.g. Association StudentAlternativeEducationProgramAssociation ...). For a Descriptor declaration, MetaEd appends Descriptor to the name (AlternativeEducationEligibilityReason becomes AlternativeEducationEligibilityReasonDescriptor). |
isDescriptor | true for any entity you declare with the Descriptor keyword in MetaEd. |
isResourceExtension | true only when extending an existing core resource rather than defining a wholly new one (see The ApiSchema Extension Model above). |
projectEndpointName and resourceName together form the URL routing segment
and the claim URI segment for each resource:
http://ed-fi.org/identity/claims/{projectEndpointName}/{resourceName}
Matching a request's resource against your claims hierarchy is
case-insensitive, so the exact casing you use when authoring claimSets
entries (see How To: Create and Manage API Security
Metadata)
doesn't affect authorization, but matching the schema's resourceName casing
keeps your claims file self-documenting.
Removing or Changing an Extension
There is no in-place uninstall for an extension. Because the Ed-Fi API validates a single fingerprint (the effective schema hash) against the database, any change to the set of installed extensions (adding, removing, or updating one) produces a new effective schema and requires a fresh database. See Upgrading the Schema for the full procedure: provision a new database for the new schema, re-load data through the Ed-Fi API, then point the service at the new database and restart it. See Schema Fingerprint Validation for what happens when the schema and the database don't match.
In a local environment:
-
Tear down the stack:
./bootstrap-local-dms.ps1 -d -v -
Stage the new schema set with
prepare-dms-schema.ps1. -
Start the stack again with
bootstrap-local-dms.ps1.
-d -v deletes the local data volumes, including all data loaded into the
local databases, and removes the staged schema workspace. You can't skip it:
prepare-dms-schema.ps1 refuses to stage a different schema set over an
existing workspace.