Skip to content

Commit 3e37b25

Browse files
committed
fix: restore full vs compact model distinction
The v0.3.0 migration accidentally made full and compact files identical by applying withCompactUris() to both. This fix restores the semantic difference from v0.2.x: - <name>.json (full): expanded URIs, source maps on by default. Intended for editing tools and validators. Source maps can be disabled via sourceMaps: false in ApiConfiguration. - <name>-compact.json: compact URIs (apiContract:WebAPI prefixes), never includes source maps. Optimized for display/browsing (e.g. API Console). Can reduce model size by up to 80% for large APIs. Both files share the same parse and transform step; only the render options differ, so there is no performance regression. Made-with: Cursor
1 parent 2b0de3a commit 3e37b25

2 files changed

Lines changed: 42 additions & 23 deletions

File tree

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ If the value is an array then first element must be API format and second is API
3030
`ApiDefinition.mime``String`. API media type. Default to `application/yaml`. For GRPC use `application/x-protobuf`.
3131
`ApiDefinition.resolution``String`. AMF resolution pipeline. Default to `editing` which is the original resolution pipeline for API Console. Future releases of AMF can support different options.
3232
`ApiDefinition.flattened``Boolean`. When `true`, generates a compact JSON-LD model using `@graph` instead of an expanded array. Recommended for large APIs. Default to `false`.
33-
`ApiDefinition.sourceMaps``Boolean`. When `false`, omits AMF source map nodes from the generated model. Source maps are only needed for editing tooling (e.g. API designers). Disabling them can reduce model size by up to 80% for large APIs like gRPC. Default to `true`.
33+
`ApiDefinition.sourceMaps``Boolean`. Controls source maps in the **full** model (`<name>.json`) only. When `false`, source map nodes are omitted — useful when no editing tooling will consume this API. The compact model (`<name>-compact.json`) never includes source maps. Default to `true`.
34+
35+
### Output files
36+
37+
Each API produces two files:
38+
39+
| File | URIs | Source maps | Intended consumer |
40+
|------|------|-------------|-------------------|
41+
| `<name>.json` | Expanded (`http://a.ml/...`) | Yes (controlled by `sourceMaps`) | Editing tools, validators |
42+
| `<name>-compact.json` | Compact (`apiContract:WebAPI`) | Never | API Console, display/browsing |
3443

3544

3645
### Example apis.json

index.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ function getConfiguration(type) {
4040
/**
4141
* Generates json/ld file from parsed document using AMF v5 API.
4242
*
43+
* Produces two output files:
44+
* - `<name>.json` — full model, expanded URIs, source maps controlled by `sourceMaps`
45+
* - `<name>-compact.json` — compact URIs, never includes source maps (optimized for display)
46+
*
4347
* @param {string} sourceFile
4448
* @param {string} file
4549
* @param {string} type
@@ -55,45 +59,51 @@ async function processFile(sourceFile, file, type, destPath, resolution, flatten
5559
dest = dest.substr(dest.lastIndexOf('/'));
5660
}
5761

58-
// Setup render options
59-
let renderOpts = new RenderOptions().withCompactUris();
60-
if (sourceMaps) {
61-
renderOpts = renderOpts.withSourceMaps();
62-
}
63-
if (flattened) {
64-
renderOpts = renderOpts.withCompactedEmission();
65-
}
66-
67-
// Get configuration for API type
68-
const apiConfiguration = getConfiguration(type).withRenderOptions(renderOpts);
69-
const client = apiConfiguration.baseUnitClient();
70-
71-
// Parse the file
72-
const parseResult = await client.parse(sourceFile);
62+
// Parse and transform using a base client (render options don't affect parsing)
63+
const parseClient = getConfiguration(type).baseUnitClient();
64+
const parseResult = await parseClient.parse(sourceFile);
7365

7466
if (!parseResult.conforms) {
7567
/* eslint-disable-next-line no-console */
7668
console.log('Validation warnings/errors:');
7769
console.log(parseResult.toString());
7870
}
7971

80-
// Transform using resolution pipeline
8172
const pipelineId = resolution === 'editing' ? PipelineId.Editing : PipelineId.Default;
82-
const transformed = client.transform(parseResult.baseUnit, pipelineId);
73+
const transformed = parseClient.transform(parseResult.baseUnit, pipelineId);
8374

84-
// Render to JSON-LD
8575
const fullFile = path.join(destPath, dest);
8676
const compactDest = dest.replace('.json', '-compact.json');
8777
const compactFile = path.join(destPath, compactDest);
8878

89-
// Generate full model (same as compact in v5 with withCompactUris)
90-
const modelData = await client.render(transformed.baseUnit, 'application/ld+json');
79+
// Full model: expanded URIs, source maps controlled by option (for editing tooling)
80+
let fullRenderOpts = new RenderOptions();
81+
if (sourceMaps) {
82+
fullRenderOpts = fullRenderOpts.withSourceMaps();
83+
}
84+
if (flattened) {
85+
fullRenderOpts = fullRenderOpts.withCompactedEmission();
86+
}
87+
const fullData = await getConfiguration(type)
88+
.withRenderOptions(fullRenderOpts)
89+
.baseUnitClient()
90+
.render(transformed.baseUnit, 'application/ld+json');
9191

9292
await fs.ensureFile(fullFile);
93-
await fs.writeFile(fullFile, modelData, 'utf8');
93+
await fs.writeFile(fullFile, fullData, 'utf8');
94+
95+
// Compact model: compact URIs, no source maps (optimized for display/browsing)
96+
let compactRenderOpts = new RenderOptions().withCompactUris();
97+
if (flattened) {
98+
compactRenderOpts = compactRenderOpts.withCompactedEmission();
99+
}
100+
const compactData = await getConfiguration(type)
101+
.withRenderOptions(compactRenderOpts)
102+
.baseUnitClient()
103+
.render(transformed.baseUnit, 'application/ld+json');
94104

95105
await fs.ensureFile(compactFile);
96-
await fs.writeFile(compactFile, modelData, 'utf8');
106+
await fs.writeFile(compactFile, compactData, 'utf8');
97107
}
98108

99109
/**

0 commit comments

Comments
 (0)