Skip to content

Commit

Permalink
add option to preserveExistingSchemas
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Jun 9, 2024
1 parent fa384d2 commit 3a4918c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
40 changes: 40 additions & 0 deletions applications/__tests__/__snapshots__/e2e.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,46 @@ components: {}

exports[`bundle > do not bundle an openapi with type never 1`] = `undefined`;

exports[`bundle > preserve existing schemas if preserveExistingSchemas is true 1`] = `
"openapi: 3.1.0
info:
title: Test
version: 1.0.0
paths:
/test:
get:
responses:
'200':
description: Test
content:
application/json:
x-type: string
schema:
type: number
components: {}
"
`;

exports[`bundle > replace existing schemas by default 1`] = `
"openapi: 3.1.0
info:
title: Test
version: 1.0.0
paths:
/test:
get:
responses:
'200':
description: Test
content:
application/json:
x-type: string
schema:
type: string
components: {}
"
`;

exports[`bundle > resolve and translate $ands 1`] = `
"openapi: 3.1.0
info:
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 @@ -83,6 +83,20 @@ describe("bundle", () => {
)
expect(stdout).toMatchSnapshot()
})

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

test("preserve existing schemas if preserveExistingSchemas is true", () => {
const {stdout: preserved} = runCommand(
"redocly bundle applications/resources/openapi-with-schema.yaml --config=applications/x-preserve-schemas-redocly.yaml"
)
expect(preserved).toMatchSnapshot()
})
})

describe("lint", () => {
Expand Down
15 changes: 15 additions & 0 deletions applications/resources/openapi-with-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
openapi: 3.1.0
info:
title: Test
version: 1.0.0
paths:
/test:
get:
responses:
200:
description: Test
content:
application/json:
x-type: string
schema:
type: number
6 changes: 6 additions & 0 deletions applications/x-preserve-schemas-redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extends:
- x-redocly.yaml

preprocessors:
x-types/generate-schemas:
preserveExistingSchemas: true
15 changes: 12 additions & 3 deletions applications/x-types-decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ const {isObject} = require("./x-types-utils")
const {translateXTypeToSchema} = require("./x-types-adapter")
const {resolveAndMerge} = require("./x-types-resolver")

const generateSchema = () => {
const generateSchema = opts => {
const preserveExistingSchemas = !!opts?.preserveExistingSchemas
return {
MediaType: {
leave(mediaType, ctx) {
const original = mediaType["x-type"]
if (typeof original === "undefined") return
if (
typeof original === "undefined" ||
(preserveExistingSchemas && mediaType.schema)
)
return
const resolvedXType = resolveAndMerge(original, ctx)
const schema = translateXTypeToSchema(resolvedXType)
mediaType.schema = schema
Expand All @@ -16,7 +21,11 @@ const generateSchema = () => {
Parameter: {
leave(parameter, ctx) {
const original = parameter["x-type"]
if (typeof original === "undefined") return
if (
typeof original === "undefined" ||
(preserveExistingSchemas && parameter.schema)
)
return
const resolvedXType = resolveAndMerge(original, ctx)
const schema = translateXTypeToSchema(resolvedXType)
parameter.schema = schema
Expand Down

0 comments on commit 3a4918c

Please sign in to comment.