Skip to content

Commit

Permalink
fix(oas3): attempt to render schemas not resolved by swagger-client (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud authored Feb 23, 2024
1 parent cfb7ca9 commit 7300e6c
Show file tree
Hide file tree
Showing 7 changed files with 63,117 additions and 7 deletions.
30 changes: 23 additions & 7 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 { Map } from "immutable"

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

Expand Down Expand Up @@ -55,20 +56,35 @@ export default class Model extends ImmutablePureComponent {
const PrimitiveModel = getComponent("PrimitiveModel")
let type = "object"
let $$ref = schema && schema.get("$$ref")
let $ref = schema && schema.get("$ref")

// 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 weren't passed a `name` but have a resolved ref, grab the name from the ref
if (!name && $$ref) {
name = this.getModelName($$ref)
}
// 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 an unresolved ref, get the schema and name from the ref.
* If the ref is external, we can't resolve it, so we just display the ref location.
* This is for situations where the ref was not resolved by Swagger Client
* because we reached the traversal depth limit.
*/
if ($ref) {
name = this.getModelName($ref)
const refSchema = this.getRefSchema(name)
if (Map.isMap(refSchema)) {
schema = refSchema.set("$$ref", $ref)
$$ref = $ref
} else {
schema = null
name = $ref
}
}

if(!schema) {
return <span className="model model-title">
<span className="model-title__text">{ displayName || name }</span>
<RollingLoadSVG height="20px" width="20px" />
{!$ref && <RollingLoadSVG height="20px" width="20px" />}
</span>
}

Expand Down
17 changes: 17 additions & 0 deletions src/core/components/object-model.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default class ObjectModel extends Component {
}
</span>)

const allOf = specSelectors.isOAS3() ? schema.get("allOf") : null
const anyOf = specSelectors.isOAS3() ? schema.get("anyOf") : null
const oneOf = specSelectors.isOAS3() ? schema.get("oneOf") : null
const not = specSelectors.isOAS3() ? schema.get("not") : null
Expand Down Expand Up @@ -194,6 +195,22 @@ export default class ObjectModel extends Component {
</td>
</tr>
}
{
!allOf ? null
: <tr>
<td>{ "allOf ->" }</td>
<td>
{allOf.map((schema, k) => {
return <div key={k}><Model { ...otherProps } required={ false }
getComponent={ getComponent }
specPath={specPath.push("allOf", k)}
getConfigs={ getConfigs }
schema={ schema }
depth={ depth + 1 } /></div>
})}
</td>
</tr>
}
{
!anyOf ? null
: <tr>
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, schemaName) => {
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, schemaName) => (system) => {
return system.getSystem().specSelectors.findSchema(schemaName)
})

export const definitions = onlyOAS3(() => (system) => {
const spec = system.getSystem().specSelectors.specJson()
const schemas = spec.getIn(["components", "schemas"])
Expand Down
37 changes: 37 additions & 0 deletions test/e2e-cypress/e2e/features/plugins/oas3/complex-spec.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @prettier
*/

describe("OpenAPI 3.0 complex spec with allOf and nested references", () => {
it("should render nested references", () => {
cy.visit("/?url=/documents/features/oas3-complex-spec.json").then(() => {
cy.get(
"[id='model-com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.StudyTreatments-create']"
)
.find("button")
.click()
cy.get(".property-row")
.contains("scenario")
.siblings()
.as("scenarioSiblings")
cy.get("@scenarioSiblings").find("button").click()
cy.get("@scenarioSiblings")
.find("span")
.contains("scenarioID")
.should("not.exist")
cy.get("@scenarioSiblings")
.find("span")
.contains("Scenarios (for create)")
.should("exist")
.click()
cy.get("@scenarioSiblings")
.find("span")
.contains("scenarioID")
.should("exist")
cy.get("@scenarioSiblings")
.find("span")
.contains("#/components/schemas/unresolvedRef")
.should("exist")
})
})
})
Loading

0 comments on commit 7300e6c

Please sign in to comment.