Skip to content

Commit 7e30ca0

Browse files
authored
Merge pull request #15 from DavidBiesack/bug/$comment-base64
Bug/$comment base64 these changes fix the issues.
2 parents fe59bc9 + 8f3feda commit 7e30ca0

8 files changed

Lines changed: 547 additions & 145 deletions

File tree

README.md

Lines changed: 98 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,25 @@ Options:
7272
--tokenUrl <tokenUrl> The tokenUrl for openIdConnect -> oauth2 transformation
7373
-d, --delete-examples-with-id If set, delete any JSON Schema examples that have an `id` property
7474
--oidc-to-oath2 <scopes> Convert openIdConnect security to oath2.
75+
--convertJsonComments If used, convert `$comment` in JSON schemas
76+
to `x-comment`. If omitted, delete
77+
all `$comment` in JSON schemas.
78+
(Use `--verbose` to log deletion
79+
to stdout)
7580
-s, --scopes <scopes> If set, this JSON/YAML file describes the OpenID scopes.
7681
This is an alias for --oidc-to-oath2
7782
-v, --verbose Verbose output
7883
-V, --version output the version number
7984
-h, --help display help for command
8085
```
8186

82-
The verbose mode logs the changes to standard error output stream.
87+
The verbose mode logs all changes to standard error output stream.
88+
89+
The tool returns a 0 status code upon success or a non-zero status code
90+
if it finds constructs that cannot be down-converted, such as
91+
using `contentMediaType: application/octet-stream` with a `format`
92+
other than `binary`, or if a schema has `contentEncoding: base64`
93+
and has an existing `format` that is not already `base64`.
8394

8495
The tool only supports local file-based documents, not URLs.
8596
Download such files to convert:
@@ -170,8 +181,8 @@ mySchema:
170181
This also applies to the schema used in parameters or in `requestBody` objects
171182
and in responses.
172183

173-
**Note** This transformation is disabled by default because it breaks `openapi-generator` 5.4 in
174-
cases where the referenced schema is an array.
184+
**Note** This transformation is disabled by default because it breaks
185+
`openapi-generator` 5.4 in cases where the referenced schema is an array.
175186
It generates Typescript types for such cases as
176187

177188
```typescript
@@ -223,7 +234,7 @@ components:
223234
const: '1.0.0'
224235
```
225236

226-
becomes
237+
becomes
227238

228239
```yaml
229240
components:
@@ -235,7 +246,7 @@ components:
235246
- '1.0.0'
236247
```
237248

238-
### Convert type arrays to nullable
249+
### &DownArrowBar; Convert type arrays to nullable
239250

240251
If a schema has a type array of exactly two values, and one of them
241252
is the string `'null'`, the type is converted to the non-null string item,
@@ -302,11 +313,11 @@ be possible (`properties`, `allOf` etc.)
302313

303314
(Contributions welcome.)
304315

305-
### Remove `unevaluatedProperties`
316+
### &DownArrowBar; Remove `unevaluatedProperties`
306317

307318
The tool removes the `unevaluatedProperties` value, introduced in later
308319
versions of JSON Schema,
309-
as this is not supported in OAS 3.0 JSON Schema Draft 4
320+
as this is not supported in JSON Schema Draft 4
310321
used in OAS 3.0.
311322

312323
```yaml
@@ -331,14 +342,27 @@ becomes
331342
...
332343
```
333344

334-
### Rename `$comment` as `x-comment`
345+
The tool removes any `$id` or `$schema` keywords that may appear
346+
inside schema objects.
347+
348+
### &DownArrowBar; Convert `$comment` to `x-comment`
349+
350+
JSON Schema introduced `$comment` in schemas in 2020-12.
351+
Since OAS 3.0 uses JSON Schema Draft 4, and some tools
352+
will flag `$comment` as invalid, this tool removes these comments.
335353

336-
The tool renames the `$comment` keyword in schema objects as `x-comment`
337-
as `$comment` is not supported in OAS 3.0 JSON Schema Draft 4
338-
used in OAS 3.0. and can cause problems with some tools.
339-
`x-comment` is more easily ignored since it does not start with `$`.
354+
An earlier version of the tool converted `$comment` to `x-comment`
355+
However, other tools which do not allow `$comment` may not not support
356+
`x-comment` either.
357+
358+
Use the `--convert-schema-comments` CLI option or set
359+
`convertSchemaComments` to `true`
360+
in the `Converter` constructor options
361+
to requst conversion of
362+
`$comment` to `x-comment` rather than deleting `$comment`.
363+
364+
For example,
340365

341-
For exmample,
342366
```yaml
343367
Problems:
344368
title: Problems
@@ -365,16 +389,70 @@ becomes
365389
$ref: '#/components/schemas/apiProblem'
366390
```
367391
368-
### Remove schema `$id` and `$schema`
392+
### Convert `contentEncoding: base64` to `format: byte`
393+
394+
JSON Schema Draft 7 and later uses `contentEncoding` to specify
395+
[the encoding of non-JSON string content]
396+
(https://json-schema.org/understanding-json-schema/reference/non_json_data).
397+
Draft 4 supports `format: byte` for `Base64` encoded strings.
398+
399+
This tool converts `type: string` schemas as follows:
400+
401+
<!-- markdownlint-disable MD033 -->
402+
<table>
403+
404+
<tr>
405+
<th>OAS 3.1 schema</th>
406+
<th>OAS 3.0 schema</th>
407+
</tr>
408+
2
409+
<tr>
410+
<td>
411+
<pre>
412+
type: string
413+
contentEncoding: base64
414+
</pre>
415+
</td>
416+
<td>
417+
<pre>
418+
type: string
419+
format: byte
420+
</pre>
421+
</td>
422+
</tr>
423+
424+
<tr>
425+
<td>
426+
<pre>
427+
type: string
428+
contentMediaType: 'application/octet-stream'
429+
</pre>
430+
</td>
431+
<td>
432+
<pre>
433+
type: string
434+
format: binary
435+
</pre>
436+
</td>
437+
</tr>
438+
</table>
369439

370-
The tool removes any `$id` or `$schema` keywords that may appear
371-
inside schema objects.
372440

373441
## Unsupported down conversions
374442

443+
Currently, the tool does not support the following situations.
444+
Contributions welcome!
445+
375446
* `openapi-down-convert` does not convert `exclusiveMinimum` and `exclusiveMaximum`
376-
as defined in JSON Schema 2012-12; these handled differently in JSON Schema Draft 4
377-
used in OAS 3.0. Contributions welcome!
378-
* Webhooks are not addressed. Contributions welcome!
447+
as defined in JSON Schema 2012-12; these handled differently in JSON Schema Draft 4
448+
used in OAS 3.0.
449+
* Webhooks are not addressed.
379450
* The tool only supports self-contained documents. It does not follow or resolve
380-
external `$ref` documents embedded in the source document.
451+
external `$ref` documents embedded in the source document.
452+
* Request body and response body `content` object transformations, such as
453+
reversing `content: { 'application/octet-stream': {} }` as
454+
described in [Migrating from OpenAPI 3.0 to 3.1.0](https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0)
455+
* Converting other `contentEncoding` values (`7bit`, `8bit`, `binary`,
456+
`quoted-printable`, `base16`, `base32`) (Note: `contentEncoding: base64` is supported by
457+
converting to `format: byte` as listed above.)
458+
* Converting `contentMediaType: 'type/subtype` to `media: { type: 'type/subtype'}` for non-JSON data.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apiture/openapi-down-convert",
3-
"version": "0.9.0",
3+
"version": "0.10.0",
44
"description": "Tool to down convert OpenAPI 3.1 to OpenAPI 3.0",
55
"main": "lib/src/index.js",
66
"bin": {

src/RefVisitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ export function visitSchemaObjects(node: any, schemaCallback: SchemaVisitor): an
6262
if (schemas != null && typeof schemas === 'object') {
6363
for (const schemaName in schemas) {
6464
const schema = schemas[schemaName];
65-
schemas[schemaName] = schemaCallback(schema);
65+
const newSchema = schemaCallback(schema);
66+
schemas[schemaName] = newSchema;
6667
}
6768
}
6869
}

src/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ async function main(args: string[] = process.argv) {
2525
.option('--oidc-to-oauth2 <scopes>', 'Convert openIdConnect security to oauth2 to allow scope definition')
2626
.option('-s, --scopes <scopes>', 'Alias for --oidc-to-oauth2')
2727
.option('-v, --verbose', 'Verbose output')
28+
.option('--convert-schema-comments', 'Convert $comment to x-comment instead of deleting $comment values')
2829
.parse(args);
2930
const opts = cli.opts();
3031
const sourceFileName: string = opts.input || 'openapi.yaml';
@@ -37,6 +38,7 @@ async function main(args: string[] = process.argv) {
3738
authorizationUrl: opts.authorizationUrl,
3839
tokenUrl: opts.tokenUrl,
3940
scopeDescriptionFile: opts.scopes,
41+
convertSchemaComments: opts.convertSchemaComments,
4042
};
4143
const converter = new Converter(source, cOpts);
4244
try {

0 commit comments

Comments
 (0)