Skip to content

Commit

Permalink
handle x-types inside parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Jun 5, 2024
1 parent b36258c commit b4569ac
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 6 deletions.
93 changes: 93 additions & 0 deletions applications/__tests__/__snapshots__/e2e.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,42 @@ components:
"
`;

exports[`bundle > bundle and translate x-type to schema inside parameters 1`] = `
"openapi: 3.1.0
info:
title: Test
version: 1.0.0
paths:
/test:
get:
parameters:
- name: correct
in: query
x-type: string
example: Test
schema:
type: string
- name: wrong-example
in: query
x-type: string
example: 42
schema:
type: string
- name: lacks-x-type
in: query
- $ref: '#/components/parameters/Referenced'
components:
parameters:
Referenced:
name: referenced-wrong-example
in: query
x-type: string
example: true
schema:
type: string
"
`;

exports[`bundle > do not add schemas if there is no x-type 1`] = `
"openapi: 3.1.0
info:
Expand Down Expand Up @@ -438,6 +474,63 @@ run \`redocly lint --generate-ignore-file\` to add all problems to the ignore fi
"
`;
exports[`lint > lints with x-types inside parameters 1`] = `
"validating applications/resources/openapi-with-x-types-inside-parameters.yaml...
[1] applications/resources/openapi-with-x-types-inside-parameters.yaml:16:20 at #/paths/~1test/get/parameters/1/example
Example value must conform to the schema: type must be string.
14 | in: query
15 | x-type: string
16 | example: 42
| ^^
17 | - name: lacks-x-type
18 | in: query
referenced from applications/resources/openapi-with-x-types-inside-parameters.yaml:13:11 at #/paths/~1test/get/parameters/1
Error was generated by the no-invalid-parameter-examples rule.
[2] applications/resources/openapi-with-x-types-inside-parameters.yaml:17:11 at #/paths/~1test/get/parameters/2
Must contain at least one of the following fields: schema, content, x-type.
15 | x-type: string
16 | example: 42
17 | - name: lacks-x-type
| ^^^^^^^^^^^^^^^^^^
18 | in: query
| ^^^^^^^^^
19 | - $ref: "#/components/parameters/Referenced"
20 | components:
Error was generated by the spec rule.
[3] applications/resources/openapi-with-x-types-inside-parameters.yaml:26:16 at #/components/parameters/Referenced/example
Example value must conform to the schema: type must be string.
24 | in: query
25 | x-type: string
26 | example: true
| ^^^^
27 |
referenced from applications/resources/openapi-with-x-types-inside-parameters.yaml:23:7 at #/components/parameters/Referenced
Error was generated by the no-invalid-parameter-examples rule.
applications/resources/openapi-with-x-types-inside-parameters.yaml: validated in <test>ms
❌ Validation failed with 3 errors.
run \`redocly lint --generate-ignore-file\` to add all problems to the ignore file.
"
`;
exports[`lint > lints x-openapi-with-refs.yaml 1`] = `
"validating applications/outputs/x-openapi-with-refs.yaml...
[1] applications/outputs/x-openapi-with-refs.yaml:23:21 at #/paths/~1test/get/responses/200/content/application~1json/examples/Incorrect/value
Expand Down
14 changes: 14 additions & 0 deletions applications/__tests__/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ describe("bundle", () => {
)
expect(stderr).toMatchSnapshot()
})

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

describe("lint", () => {
Expand Down Expand Up @@ -81,4 +88,11 @@ describe("lint", () => {
)
expect(stripCWD(stderr)).toMatchSnapshot()
})

test("lints with x-types inside parameters", () => {
const {stderr} = runCommand(
"redocly lint applications/resources/openapi-with-x-types-inside-parameters.yaml --config=applications/x-redocly.yaml"
)
expect(stderr).toMatchSnapshot()
})
})
26 changes: 26 additions & 0 deletions applications/resources/openapi-with-x-types-inside-parameters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.1.0
info:
title: Test
version: 1.0.0
paths:
/test:
get:
parameters:
- name: correct
in: query
x-type: string
example: Test
- name: wrong-example
in: query
x-type: string
example: 42
- name: lacks-x-type
in: query
- $ref: "#/components/parameters/Referenced"
components:
parameters:
Referenced:
name: referenced-wrong-example
in: query
x-type: string
example: true
3 changes: 2 additions & 1 deletion applications/x-redocly.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
rules:
spec: error
no-invalid-media-type-examples: error
no-unresolved-refs: error
no-invalid-media-type-examples: error
no-invalid-parameter-examples: error

extends:
- x-types/all
Expand Down
12 changes: 9 additions & 3 deletions applications/x-types-decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ const generateSchema = () => {
MediaType: {
leave(mediaType, ctx) {
if (typeof mediaType["x-type"] === "undefined") return

const schema = translateXTypeToSchema(mediaType["x-type"], ctx)
mediaType.schema = schema
},
},
Parameter: {
leave(parameter, ctx) {
if (typeof parameter["x-type"] === "undefined") return
const schema = translateXTypeToSchema(parameter["x-type"], ctx)
parameter.schema = schema
},
},
}
}

const create$Refs = () => {
const createRefs = () => {
return {
any: {
enter: (node, ctx) => {

Check warning on line 26 in applications/x-types-decorators.js

View workflow job for this annotation

GitHub Actions / code-style

'ctx' is defined but never used
Expand All @@ -34,5 +40,5 @@ const create$Refs = () => {

module.exports = {
generateSchema,
create$Refs,
createRefs,
}
12 changes: 10 additions & 2 deletions applications/x-types-plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {generateSchema, create$Refs} = require("./x-types-decorators")
const {generateSchema, createRefs} = require("./x-types-decorators")
const {noRefNeighbors} = require("./x-types-rules")

const getType = value => {
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = {

preprocessors: {
oas3: {
"create-$refs": create$Refs,
"create-$refs": createRefs,
},
},

Expand Down Expand Up @@ -114,6 +114,14 @@ module.exports = {
"x-type": getType,
},
},
Parameter: {
...types.Parameter,
properties: {
...types.Parameter.properties,
"x-type": getType,
},
requiredOneOf: ["schema", "content", "x-type"],
},
Components: {
...types.Components,
properties: {
Expand Down

0 comments on commit b4569ac

Please sign in to comment.