Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions applications/__tests__/__snapshots__/e2e-bundle.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,20 @@ components:
enum:
- az
- bukh
[1] applications/resources/openapi-with-refs.yaml:61:19 at #/paths/~1test/get/responses/404/content/application~1json/x-type/AWrongRef

Can't resolve $ref: ENOENT: no such file or directory './applications/resources/wrong-file.yaml'

59 | x-type:
60 | AWrongRef:
61 | $ref: wrong-file.yaml
| ^^^^^^^^^^^^^^^^^^^^^
62 | example:
63 | AWrongRef: Accepts anything

Error was generated by the bundler rule.


"
`;

Expand Down
362 changes: 15 additions & 347 deletions applications/__tests__/__snapshots__/e2e-lint.test.js.snap

Large diffs are not rendered by default.

42 changes: 22 additions & 20 deletions applications/__tests__/e2e-bundle.test.js
Original file line number Diff line number Diff line change
@@ -1,138 +1,140 @@
import {describe, expect, test} from 'vitest'
import {runCommand} from './e2e-utils'
import {runCommand, stripCWD} from './e2e-utils.js'

describe('bundle', () => {
test('bundle and translate x-type to schema (for regular $ref objects)', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('do not add schemas if there is no x-type', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-without-x-types.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('resolve different type of $refs on different levels and ignore wrong $refs (with --force) when bundling', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-with-refs.yaml --force --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('do not bundle an openapi with type never', () => {
const {stderr} = runCommand(
'redocly bundle applications/resources/openapi-never.yaml --config=applications/x-type.redocly.yaml'
)
expect(stderr).toMatchSnapshot()
expect(stripCWD(stderr)).toMatchSnapshot()
})

test('resolve and translate $ands', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-and.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('translate x-type to schema inside parameters', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-with-x-types-inside-parameters.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('translate x-types inside ORs', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-or.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('translate string and number formats', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-type-formats.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('distributivity in x-types', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-with-nested-ors-in-ands.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('replace existing schemas', () => {
const {stdout: notPreserved} = runCommand(
'redocly bundle applications/resources/openapi-with-schema.yaml --config=applications/x-type.redocly.yaml'
)
expect(notPreserved).toMatchSnapshot()
expect(stripCWD(notPreserved)).toMatchSnapshot()
})

test('descriptions in x-types', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-with-descriptions.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('generate x-types from JSON Schemas (petstore)', () => {
const {stdout: toXTypes} = runCommand(
'redocly bundle applications/resources/pets.yaml --config=applications/generate-x-types.redocly.yaml'
)
expect(toXTypes).toMatchFileSnapshot('file-snapshots/pets-to-x-types.yaml')
expect(stripCWD(toXTypes)).toMatchFileSnapshot(
'file-snapshots/pets-to-x-types.yaml'
)
})

test('generate x-types from JSON Schemas (readOnly and writeOnly)', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-from-readonly-writeonly-to-omit.yaml --config=applications/generate-x-types.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with circular refs to writeOnly', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-writeonly-circular.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with omitting fields', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-omit.yaml --config=applications/x-type.redocly.yaml'
)
// FIXME: the $omit field merges directly into the resolved object. this may cause issues if $ref is not an object
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with $-prefixed fields', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-dollar-prefixed-fields.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with $and and $description', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-and-with-descriptions.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with discriminators converted to x-types', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-with-discriminators.yaml --config=applications/generate-x-types.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with circular refs converted to schemas', () => {
const {stdout} = runCommand(
'redocly bundle applications/resources/openapi-with-circular-refs.yaml --config=applications/x-type.redocly.yaml'
)
expect(stdout).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})
})
50 changes: 25 additions & 25 deletions applications/__tests__/e2e-lint.test.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
import {describe, expect, test} from 'vitest'
import {runCommand, stripCWD} from './e2e-utils'
import {runCommand, stripCWD} from './e2e-utils.js'

describe('lint', () => {
test('general openapi case (using preprocessors to transform)', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi.yaml --config=applications/x-type.redocly.yaml'
)
expect(stderr).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with mixed types', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi-mixed-types.yaml --config=applications/x-type.redocly.yaml'
)
expect(stripCWD(stderr)).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi that contains wrong and correct $ands', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi-and.yaml --config=applications/x-type.redocly.yaml'
)
expect(stripCWD(stderr)).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with x-types inside parameters', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi-with-x-types-inside-parameters.yaml --config=applications/x-type.redocly.yaml'
)
expect(stderr).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with ORs (including nested and referenced)', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi-or.yaml --config=applications/x-type.redocly.yaml'
)
expect(stderr).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('distribytivity in x-types', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi-with-nested-ors-in-ands.yaml --config=applications/x-type.redocly.yaml'
)
expect(stderr).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('x-type described with x-type itself', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/x-types-described-with-x-type.yaml --config=applications/x-type.redocly.yaml'
)
expect(stripCWD(stderr)).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with external $refs', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi-with-external-refs.yaml --config=applications/x-type.redocly.yaml'
)
expect(stripCWD(stderr)).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with omitting fields', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi-omit.yaml --config=applications/x-type.redocly.yaml'
)
expect(stripCWD(stderr)).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with $and and $description', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi-and-with-descriptions.yaml --config=applications/x-type.redocly.yaml'
)
expect(stripCWD(stderr)).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with circular refs', () => {
const {stderr} = runCommand(
const {stdout} = runCommand(
'redocly lint applications/resources/openapi-with-circular-refs.yaml --config=applications/x-type.redocly.yaml'
)
expect(stripCWD(stderr)).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})

test('openapi with redundant key in $descriptions', () => {
const {stdout: stderr} = runCommand(
const {stdout: stdout} = runCommand(
'redocly lint applications/resources/openapi-with-redundant-description.yaml --config=applications/x-type.redocly.yaml 2>&1'
)
expect(stripCWD(stderr)).toMatchSnapshot()
expect(stripCWD(stdout)).toMatchSnapshot()
})
})
5 changes: 3 additions & 2 deletions applications/__tests__/e2e-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export const runCommand = cmd => {
})
stdout = out.toString('utf-8')
} catch (err) {
stderr = err.stderr.toString('utf-8')
stdout = err.stdout?.toString('utf-8') || ''
stderr = err.stderr?.toString('utf-8') || ''
}
return {stderr, stdout}
}

export const stripCWD = str => str.replace(process.cwd(), '.')
export const stripCWD = str => str?.replace(process.cwd(), '.') ?? str
3 changes: 1 addition & 2 deletions applications/json-schema-adapter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Experimental feature

import {isRef} from '@redocly/openapi-core'
import {isPlainObject, isEmptyObject} from '@redocly/openapi-core/lib/utils.js'
import {isRef, isPlainObject, isEmptyObject} from '@redocly/openapi-core'
import {RESERVED_KEYWORDS} from './x-types-utils.js'

const escapeReserved = value => {
Expand Down Expand Up @@ -110,7 +109,7 @@
let $omit
if (isPlainObject(resolvedNode?.properties)) {
$omit = Object.entries(resolvedNode.properties)
.filter(([key, value]) => {

Check warning on line 112 in applications/json-schema-adapter.js

View workflow job for this annotation

GitHub Actions / code-style

'key' is defined but never used
const propNode = elevateProp(value)
return (
(propNode?.readOnly === true && ctx._mode === 'request') ||
Expand Down
2 changes: 1 addition & 1 deletion applications/x-type.redocly.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
rules:
spec: error
struct: error
no-unresolved-refs: error
no-invalid-media-type-examples: error
no-invalid-parameter-examples: error
Expand Down
2 changes: 1 addition & 1 deletion applications/x-types-adapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isNotEmptyObject} from '@redocly/openapi-core/lib/utils.js'
import {isNotEmptyObject} from '@redocly/openapi-core'
import {isObject} from './x-types-utils.js'

const SUFFIXES = {
Expand Down
2 changes: 1 addition & 1 deletion applications/x-types-plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isRef} from '@redocly/openapi-core/lib/ref-utils.js'
import {isRef} from '@redocly/openapi-core'
import {
generateSchemas,
generateNamedSchemas,
Expand Down
2 changes: 1 addition & 1 deletion applications/x-types-resolver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isRef} from '@redocly/openapi-core/lib/ref-utils.js'
import {isRef} from '@redocly/openapi-core'
import {isObject, mergeAll} from './x-types-utils.js'

const omit = (maybeObj, keys) => {
Expand Down
2 changes: 1 addition & 1 deletion applications/x-types-rules.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isNotEmptyObject} from '@redocly/openapi-core/lib/utils.js'
import {isNotEmptyObject} from '@redocly/openapi-core'

export const noRefNeighbors = () => {
return {
Expand Down
Loading
Loading