Skip to content

Commit 855e120

Browse files
authored
docs: improve migration information (#199)
1 parent 86ee8a5 commit 855e120

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

migrations/Migrate to version 3.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Migrating to version 3
22

3-
In version 3, the only breaking change is that the provided bundled schemas located under [schemas](../schemas) now use completely different `$id`s and definition namings.
3+
In version 3, we wanted to improve how you make changes to the JSON schema documents and improve maintainability. This was done through splitting the schemas into smaller files and then on release bundle them together to form what is called a Compound Schema Document.
44

5-
> If you don't manually access schemas under `definitions`, you can upgrade the version without any problems.
5+
To read more about bundling in JSON Schema, [we refer to the official documentation](https://json-schema.org/understanding-json-schema/structuring.html#bundling).
66

7-
Previously, each definition would have it defined as:
7+
## Change of `$id`s
8+
9+
Previously, each definition would be defined as:
810

911
```json
1012
{
@@ -33,3 +35,47 @@ With the new naming approach, those definitions would be called:
3335
```
3436

3537
All definitions follow the same change from `<name>` to `http://asyncapi.com/definitions/<version>/<name>.json`.
38+
## References
39+
Because the `$id`s changes, so must the references and this might be a problem for some implementations if they do not follow JSON Schema behavior where they can auto map loaded schemas to references.
40+
41+
```json
42+
{
43+
...
44+
"patternProperties": {
45+
"^x-[\\w\\d\\.\\x2d_]+$": {
46+
"$ref": "http://asyncapi.com/definitions/2.3.0/specificationExtension.json"
47+
}
48+
},
49+
"definitions": {
50+
"http://asyncapi.com/definitions/2.2.0/specificationExtension.json": { ... },
51+
...
52+
}
53+
}
54+
```
55+
56+
## Bundled schemas
57+
Because the provided schemas are now bundled together some validation tools **might** find this a problem, for example [Ajv](#ajv-example).
58+
59+
### Ajv example
60+
Because the AsyncAPI JSON Schema documents are written with a [specific meta schema](https://github.com/asyncapi/spec-json-schemas/blob/5d6ea0361a5b30707afa67a2df28e2805095c10f/schemas/2.3.0.json#L3). That meta schema, is already loaded by [Ajv](https://ajv.js.org), and when you try to load the bundled AsyncAPI JSON Schema, it now contains that same meta schema. Ajv does not like to load duplicate schemas and simply throw an error when you try to, and there are no way to tell it to ignore it. Therefore for Ajv you **must** remove it before using it.
61+
62+
Example code:
63+
```js
64+
const Ajv = require('ajv');
65+
const asyncapi = require('@asyncapi/specs');
66+
const ajv = new Ajv({
67+
jsonPointers: true,
68+
allErrors: true,
69+
schemaId: 'auto',
70+
logger: false
71+
});
72+
const asyncapiSchema = asyncapi['2.0.0'];
73+
// Remove the meta schemas because it is already present within Ajv, and it's not possible to add duplicate schemas.
74+
delete asyncapiSchema.definitions['http://json-schema.org/draft-07/schema'];
75+
//delete asyncapiSchema.definitions['http://json-schema.org/draft-04/schema']; <-- This is needed if you use AsyncAPI > v2
76+
ajv.addSchema(asyncapiSchema, version);
77+
validate = ajv.getSchema(version);
78+
const validAsyncAPI = validate(...);
79+
```
80+
81+
[Here is how we migrated our JS parser](https://github.com/asyncapi/parser-js/pull/423).

0 commit comments

Comments
 (0)