Import an OData service
The OData importer (odata) reads an OData service's metadata into the
Catalog. Entity types and complex types become types, enums become enums, each
entity set becomes a set of operations, and every function and action becomes an operation of its
own. You can then search, version, diff, lint and convert it like any other catalog item.
When to use it
Use the OData importer for any service that publishes OData metadata: OData v4 and 4.01, and the older v2 and v3 services that SAP Gateway and many Microsoft products still expose. Bring in the metadata as a CSDL XML or CSDL JSON file, or point Apiome at the running service.
At a glance
| Step | Where | Details |
|---|---|---|
Import a CSDL file or a service's $metadata | Import to catalog → File Upload or URL Import, or apiome import odata | Import it |
| Discover a running service from its service root | URL Import with the service root, or apiome import odata --discover | Discover a live service |
| Check the metadata against the OASIS schemas, references and keys | Automatic on every import; problems show in lint | How the metadata is checked |
| Compare two versions with the OData model-versioning rules | Tick two versions on the catalog item's Versions tab | Breaking changes between versions |
| Convert to an OpenAPI project | Convert to OpenAPI on the catalog item | Per-format conversion packs |
| Export back to OData | Export on the catalog item | OData (CSDL) from any source |
An item imported from OData exports back to OData with its entity sets, keys, types, functions and actions unchanged.
Import it
Import the metadata like any other catalog format: in the import wizard
(Import to catalog, then File Upload or URL Import), over the REST API (see
Import a specification), or from the CLI with apiome import odata <file>.
| What you have | How it is read |
|---|---|
CSDL XML: a $metadata document or .edmx file, OData v4, v3 or v2 | Read by the CSDL XML reader. v2 and v3 associations become v4-style navigation properties. |
CSDL JSON: the OData v4 JSON metadata format (.json, "$Version": "4.0" or "4.01") | Read into the same model as CSDL XML, so both forms of one service import alike. |
Functions and actions
Every function and action the metadata declares is imported as an operation, with its parameters and return type. They are grouped under one service named after the entity container (for example ShopService), beside the entity sets.
| What the metadata declares | What the import keeps |
|---|---|
A function (<Function>, or "$Kind": "Function" in CSDL JSON) | A GET operation. Its parameters are query parameters, required when declared Nullable="false". A composable function is recorded as composable. |
An action (<Action>) | A POST operation. Its parameters are listed as the request body, since an action takes them in the body. An action with no return type answers 204. |
A bound operation (IsBound="true") | Called through an entity set of its binding type: /Products/Shop.Cheapest for a collection, /Products({key})/Shop.Discount for one entity. The binding parameter is the path, not a request parameter. |
| A function import or action import in the entity container | The route of the unbound operation it names (/Search) and the entity set its result belongs to. An unbound operation nothing imports is kept, with no route. |
A v2 or v3 <FunctionImport> | An operation at /{Name}, called with its m:HttpMethod (POST when a v3 import is side-effecting, otherwise GET). Every parameter is a query parameter. |
Overloads. OData lets one name have several signatures. Each overload is kept separately, named
by its kind, its fully-qualified name and its parameter types in order — for example
Function Shop.Search(Edm.String,Edm.Int32) — so two overloads never merge, and an overload keeps its
identity from one version to the next as long as its signature does.
CSDL XML and CSDL JSON give the same operations: an unstated Nullable means nullable in XML (except
on a collection, where it is about the items) and not nullable in JSON, and both are read that way.
Discover a live service
You can import a running OData service from its service root (for example
https://services.example.com/odata/trippin/) or its $metadata URL, in the import wizard or from
the CLI. Either way the $metadata URL is <service root>/$metadata, and a query string such as
SAP's ?sap-client=100 is kept.
In the import wizard
- On the Catalog, click “Import to catalog” and choose URL Import.
- Enter the service root as the Document URL and click “Fetch and detect”.
The service root answers with its service document, the list of its entity sets, which has no
model to import. The wizard recognizes it (v4 or v3 JSON, v2 or v3 verbose JSON, or v2 or v3 Atom
XML), fetches the service's $metadata instead, and continues with that document. The catalog item
records the $metadata URL as its source. The fetch runs in your browser, so the service must allow
requests from Apiome (CORS); if it does not, import the metadata from the CLI or upload a saved copy.
From the CLI
Give the service root (or its $metadata URL) to --discover:
apiome import odata --discover https://services.example.com/odata/trippin/
apiome import odata --discover "https://sap.example.com/sap/opu/odata/sap/ZBP_SRV/?sap-client=100"
The CLI derives the $metadata URL (<service root>/$metadata, query string kept), reports it
(Discovered OData $metadata at …) and imports that document exactly as it would a --url import:
it is parsed, checked, cataloged and linted. --discover must be the
only input.
| Problem | What you see |
|---|---|
The URL is not http/https or has no host | The CLI stops with a usage error. |
$metadata cannot be fetched (404, 401, network) | The CLI stops with the fetch error; check the service root and your access. |
In the wizard, a $metadata that cannot be fetched shows Couldn't read that source with the
error (for example Fetch failed with HTTP 404.); click “Try again” once the service is
reachable.
The CLI's test fixtures include one service of each kind the importer reads most: an OData v4
service (bookshop-odata-v4.edmx) and an SAP Gateway-style v2 service (sales-odata-v2.xml), in
apiome-cli/tests/fixtures/. They are a quick way to try an import:
apiome import odata apiome-cli/tests/fixtures/sales-odata-v2.xml
How the metadata is checked
Every OData document is checked three ways. None of the checks refuses an import: a document with problems still imports, its problems are recorded on the catalog item, and lint reports them.
| Check | Versions | What it catches | Lint rule |
|---|---|---|---|
| OASIS CSDL JSON schema | v4 | A member of the wrong shape, such as a $Nullable that is not true or false | odata.csdl-schema (warning) |
| OASIS CSDL XML schema | v4 CSDL XML | XML the JSON form never sees: a misspelled attribute (Nulable), an element CSDL does not define, a missing required attribute | odata.csdl-xsd (warning) |
| References and keys | v4, v3, v2 | A name that points at nothing, and an entity type with no usable key | odata.unresolved-reference and odata.entity-key (errors) |
OASIS schemas
The two OASIS schemas describe CSDL's shape:
- CSDL JSON is checked against the CSDL JSON schema as uploaded.
- CSDL XML is checked against the OASIS
EDMX and EDM XML schemas.
It is also converted to its CSDL JSON form, following the OASIS mapping, and that form is checked
against the JSON schema and kept with the catalog item. For example, an XML property with no
Nullablebecomes"$Nullable": true, because the JSON form defaults tofalse.
v2 and v3 metadata has no OASIS schema, so it is not checked this way. The schemas are part of Apiome: checking never fetches anything, even when a document names a schema location.
References and keys
A document can be the right shape and still name things that do not exist. Apiome follows every name a document uses:
- Types: property, parameter, return and term types, base types, and the entity type of each
entity set and singleton. Names written with a schema alias (
self.Product) resolve like namespace-qualified ones. - Relationships: each navigation property's target type, partner and referential constraint; each navigation property binding's path and target; in v2 and v3, each association's ends and each association set's association, roles and entity sets.
- Operations: the action or function each import names, and the entity set it returns into.
Every entity type must also have a usable key: a non-abstract type declares one or inherits one from its base type (a derived type must not declare its own), and each key property must exist and be a single, non-nullable primitive, enumeration or type-definition value. A floating-point, stream or spatial property cannot be part of a key.
A name in a namespace the document only references (<edmx:Reference>) and did not bring in is
not checked, because the import cannot see it.
Both rules are errors, so a document that breaks them fails a style guide's publish gate. Clients generated from such metadata cannot bind the missing names or address the keyless entities.
Where the problems are recorded
Each problem carries the path of the place it is about, written like a CSDL JSON pointer
(/Namespace/Type/Member):
| Field | What it holds |
|---|---|
odata_csdl_valid | true when a v4 document matches the OASIS CSDL JSON schema |
odata_csdl_violations | Up to 50 JSON schema problems, each a path and a message, such as /Shop/Item/ID/$Nullable: 'yes' is not of type 'boolean' |
odata_csdl_violation_count | How many JSON schema problems there were in all |
odata_xsd_violations | Up to 50 XML schema problems, such as /Shop/Item/ID: 'Nulable' attribute not allowed for element |
odata_xsd_violation_count | How many XML schema problems there were in all |
odata_resolution_issues | Up to 50 reference and key problems, each a kind (reference or key), a path and a message, such as /Shop/Svc/Items/$Type: entity set 'Items' names `Shop.Itm`, which is not declared. |
odata_resolution_issue_count | How many reference and key problems there were in all |
The odata_xsd_* and odata_resolution_* fields are present only when there is a problem.
Lint reports each problem at its path: csdl:<path> for JSON schema, reference and key problems,
and xml:<path> for XML schema problems. To stop services that do not match the OASIS schemas
from being published, raise odata.csdl-schema and odata.csdl-xsd to error in a copy of your
style guide.
Breaking changes between versions
OData defines which model changes a service may make without breaking its clients: OData 4.01 Protocol, section 5.2 Model Versioning. When you compare two versions of an OData catalog item, each change is graded by those rules:
| Change | Severity | Rule |
|---|---|---|
| A nullable property added | Safe | odata.property-added-nullable |
| A non-nullable property added with a default value | Safe | odata.property-added-with-default |
| A non-nullable property added with no default | Breaking | odata.property-added-required |
| A property's type changed | Breaking | odata.property-type-changed |
| A property made non-nullable | Breaking | odata.property-made-non-nullable |
| A property made nullable | Dangerous | odata.property-made-nullable |
| An entity type's key changed | Breaking | odata.key-changed |
| A collection-valued or nullable navigation property added | Safe | odata.navigation-added |
| A single-valued, non-nullable navigation property added | Breaking | odata.navigation-added-required |
| A single-valued navigation property added whose nullability is not recorded | Dangerous | odata.navigation-added-unknown |
| A navigation property removed | Breaking | odata.navigation-removed |
| A navigation property's target type changed | Breaking | odata.navigation-type-changed |
| A navigation property's partner changed | Dangerous | odata.navigation-partner-changed |
| Annotations added | Safe | odata.annotation-added |
| An annotation changed or removed | Dangerous | odata.annotation-changed |
New entity types, complex types, enumerations, entity sets, functions and actions are safe; anything
removed is breaking. Because an overload is identified by its signature, changing a function's or
action's parameters reads as the old overload removed (breaking) and a new one added (safe). Every
other change is graded by the standard rules. A v2 or v3 navigation property's
nullability comes from its association end (0..1 is nullable, 1 is not).
Schema version. Section 5.2 asks a service that makes a breaking change to version its metadata
with the Core.SchemaVersion annotation, so clients can still ask for the old model with
$schemaversion. Each breaking change says whether the schema's Core.SchemaVersion moved between the
two versions (for example Core.SchemaVersion moved (Shop: '1' → '2')), or that it did not and should
have. The grade itself does not change. The schema version is read from v4 metadata.
When the import refuses a document
| Code | Why | What to do |
|---|---|---|
INPUT_MALFORMED | The XML or JSON cannot be read. | Fix the syntax the message names. |
INPUT_SEMANTIC_INVALID | The document declares no entity container, so it describes no service. | Add the EntityContainer (XML) or $Kind: "EntityContainer" member (JSON). |
Related
- Import a specification: the import paths every format shares
- Supported formats: OData's version coverage and export support
- Built-in lint rules: every rule with its rationale