Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve unresolved schemas #9629

Merged
merged 7 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 20 additions & 8 deletions src/core/components/model.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react"
import ImmutablePureComponent from "react-immutable-pure-component"
import ImPropTypes from "react-immutable-proptypes"
import PropTypes from "prop-types"
import { OrderedMap } from "immutable"

import RollingLoadSVG from "core/assets/rolling-load.svg"

Expand Down Expand Up @@ -54,15 +55,26 @@ export default class Model extends ImmutablePureComponent {
const ArrayModel = getComponent("ArrayModel")
const PrimitiveModel = getComponent("PrimitiveModel")
let type = "object"
let $$ref = schema && schema.get("$$ref")
let $ref = schema && schema.get("$ref")
let allOf = schema && schema.get("allOf")

// If we weren't passed a `name` but have a ref, grab the name from the ref
if ( !name && $$ref ) {
name = this.getModelName( $$ref )
// If we have a ref, get the schema and name from the ref
if ($ref) {
name = this.getModelName($ref)
schema = this.getRefSchema(name)
}
// If we weren't passed a `schema` but have a ref, grab the schema from the ref
if ( !schema && $$ref ) {
schema = this.getRefSchema( name )

// If we have allOf with refs, get the schemas and merge them
if (allOf) {
schema = allOf.reduce((acc, schema) => {
if (schema.get("$ref")) {
const refName = this.getModelName(schema.get("$ref"))
let refSchema = this.getRefSchema(refName)
refSchema = refSchema.delete("title")
return acc.mergeDeep(refSchema)
}
return acc.mergeDeep(schema)
}, new OrderedMap())
}

if(!schema) {
Expand All @@ -73,7 +85,7 @@ export default class Model extends ImmutablePureComponent {
}

const deprecated = specSelectors.isOAS3() && schema.get("deprecated")
isRef = isRef !== undefined ? isRef : !!$$ref
isRef = isRef !== undefined ? isRef : !!$ref
type = schema && schema.get("type") || type

switch(type) {
Expand Down
10 changes: 10 additions & 0 deletions src/core/plugins/oas3/spec-extensions/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ export const servers = onlyOAS3(() => (system) => {
return spec.get("servers", map)
})

export const findSchema = (state, doubledState, schemaName) => {
char0n marked this conversation as resolved.
Show resolved Hide resolved
const resolvedSchema = state.getIn(
["resolvedSubtrees", "components", "schemas", schemaName],
null
)
const unresolvedSchema = state.getIn(["json", "components", "schemas", schemaName], null)

return resolvedSchema || unresolvedSchema || null
}

export const callbacksOperations = onlyOAS3(
(state, { callbacks, specPath }) =>
(system) => {
Expand Down
4 changes: 4 additions & 0 deletions src/core/plugins/oas3/spec-extensions/wrap-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const OAS3NullSelector = onlyOAS3(nullSelector)
* Wrappers
*/

export const findDefinition = onlyOAS3((state, doubledState, schemaName) => (system) => {
char0n marked this conversation as resolved.
Show resolved Hide resolved
char0n marked this conversation as resolved.
Show resolved Hide resolved
return system.getSystem().specSelectors.findSchema(state, doubledState, schemaName)
char0n marked this conversation as resolved.
Show resolved Hide resolved
})

export const definitions = onlyOAS3(() => (system) => {
const spec = system.getSystem().specSelectors.specJson()
const schemas = spec.getIn(["components", "schemas"])
Expand Down
62 changes: 62 additions & 0 deletions test/unit/core/plugins/oas3/selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @prettier
*/
import { fromJS } from "immutable"
import { findSchema } from "core/plugins/oas3/spec-extensions/selectors"

describe("findSchema", function () {
const state = fromJS({
resolvedSubtrees: {
components: {
schemas: {
resolvedSchema: {
type: "object",
properties: {
name: {
type: "string",
},
},
},
},
},
},
json: {
components: {
schemas: {
unresolvedSchema: {
$ref: "#/components/schemas/resolvedSchema",
},
resolvedSchema: {
type: "object",
properties: {
name: {
type: "string",
},
},
},
},
},
},
})

it("should get an unresolved schema", function () {
const result = findSchema(state, null, "unresolvedSchema")

expect(result).toEqual(
state.getIn(["json", "components", "schemas", "unresolvedSchema"])
)
})

it("should get a resolved schema", function () {
const result = findSchema(state, null, "resolvedSchema")

expect(result).toEqual(
state.getIn([
"resolvedSubtrees",
"components",
"schemas",
"resolvedSchema",
])
)
})
})
Loading