Skip to content

Commit

Permalink
fix(oas31): render responses with empty content field (#9664)
Browse files Browse the repository at this point in the history
Refs #9199
  • Loading branch information
char0n committed Mar 6, 2024
1 parent 4b4760d commit e2be707
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/core/components/response.jsx
Expand Up @@ -8,20 +8,16 @@ import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"


const getExampleComponent = ( sampleResponse, HighlightCode, getConfigs ) => {
if (
sampleResponse !== undefined &&
sampleResponse !== null
) {
let language = null
let testValueForJson = getKnownSyntaxHighlighterLanguage(sampleResponse)
if (testValueForJson) {
language = "json"
}
return <div>
if (sampleResponse == null) return null

const testValueForJson = getKnownSyntaxHighlighterLanguage(sampleResponse)
const language = testValueForJson ? "json" : null

return (
<div>
<HighlightCode className="example" getConfigs={ getConfigs } language={ language } value={ stringify(sampleResponse) } />
</div>
}
return null
)
}

export default class Response extends React.Component {
Expand Down Expand Up @@ -171,7 +167,7 @@ export default class Response extends React.Component {
shouldOverrideSchemaExample ? mediaTypeExample : undefined
)

let example = getExampleComponent( sampleResponse, HighlightCode, getConfigs )
const example = getExampleComponent( sampleResponse, HighlightCode, getConfigs )

return (
<tr className={ "response " + ( className || "") } data-code={code}>
Expand Down
3 changes: 3 additions & 0 deletions src/core/plugins/json-schema-2020-12-samples/fn/main.js
Expand Up @@ -21,6 +21,9 @@ export const sampleFromSchemaGeneric = (
exampleOverride = undefined,
respectXML = false
) => {
// there is nothing to generate schema from
if (schema == null && exampleOverride === undefined) return undefined

if (typeof schema?.toJS === "function") schema = schema.toJS()
schema = typeCast(schema)

Expand Down
4 changes: 4 additions & 0 deletions src/core/plugins/oas31/after-load.js
Expand Up @@ -27,6 +27,10 @@ function afterLoad({ fn, getSystem }) {
createXMLExample: fn.jsonSchema202012.createXMLExample,
memoizedSampleFromSchema: fn.jsonSchema202012.memoizedSampleFromSchema,
memoizedCreateXMLExample: fn.jsonSchema202012.memoizedCreateXMLExample,
getJsonSampleSchema: fn.jsonSchema202012.getJsonSampleSchema,
getYamlSampleSchema: fn.jsonSchema202012.getYamlSampleSchema,
getXmlSampleSchema: fn.jsonSchema202012.getXmlSampleSchema,
getSampleSchema: fn.jsonSchema202012.getSampleSchema,
},
getSystem()
)
Expand Down
@@ -0,0 +1,21 @@
/**
* @prettier
*/

describe("OpenAPI 3.1.0 response with empty content", () => {
it("should render a response", () => {
cy.visit(
"/?configUrl=/configs/oas31-response-no-content.yaml&url=/documents/features/oas31-response-empty-content.yaml"
)
.get("#operations-Enterprise-get_enterprise_detail")
.click()
.get(
"#operations-Enterprise-get_enterprise_detail [data-code=404] .response-col_description__inner"
)
.contains("No enterprise matching the requested ID could be found.")
.get(
"#operations-Enterprise-get_enterprise_detail [data-code=404] .model-example"
)
.should("not.exist")
})
})
@@ -0,0 +1,21 @@
/**
* @prettier
*/

describe("OpenAPI 3.1.0 response with empty Media Type Object", () => {
it("should render a response", () => {
cy.visit(
"/?configUrl=/configs/oas31-response-no-content.yaml&url=/documents/features/oas31-response-empty-media-type.yaml"
)
.get("#operations-Enterprise-get_enterprise_detail")
.click()
.get(
"#operations-Enterprise-get_enterprise_detail [data-code=404] .response-col_description__inner"
)
.contains("No enterprise matching the requested ID could be found.")
.get(
"#operations-Enterprise-get_enterprise_detail [data-code=404] .model-example"
)
.should("not.exist")
})
})
@@ -0,0 +1,21 @@
/**
* @prettier
*/

describe("OpenAPI 3.1.0 response without content", () => {
it("should render a response", () => {
cy.visit(
"/?configUrl=/configs/oas31-response-no-content.yaml&url=/documents/features/oas31-response-no-content.yaml"
)
.get("#operations-Enterprise-get_enterprise_detail")
.click()
.get(
"#operations-Enterprise-get_enterprise_detail [data-code=404] .response-col_description__inner"
)
.contains("No enterprise matching the requested ID could be found.")
.get(
"#operations-Enterprise-get_enterprise_detail [data-code=404] .model-example"
)
.should("not.exist")
})
})
@@ -0,0 +1,3 @@
defaultModelRendering: "model"
defaultModelExpandDepth: 0
defaultModelsExpandDepth: -1
@@ -0,0 +1,91 @@
openapi: '3.1.0'

info:
title: Our API
description: extended description of Our API
version: 0.1.0

servers:
- url: http://localhost
description: included for completeness; not our actual url

tags:
- name: Enterprise
description: Operations with Enterprise ID

paths:
/v0/enterprise/{id}:
get:
tags:
- Enterprise
summary: Get detailed info about an enterprise.
description: |
Returns detailed information for the specified Enterprise ID.
This operation may *only* be performed by a **platform admin** or a
client with sufficient permissions.
operationId: get_enterprise_detail
parameters:
- $ref: '#/components/parameters/traceIDHeader'
- $ref: '#/components/parameters/enterpriseIDPath'
responses:
404:
description: No enterprise matching the requested ID could be found.
content: {}
components:
parameters:
enterpriseIDPath:
$ref: '#/components/parameters/idInPath'
description: The Enterprise ID used to perform this request.
example: "12422"

idInPath:
name: id
in: path
required: true
schema:
type: string

traceIDHeader:
name: X-Trace-Id
in: header
description: Optional UUID for log tracing
required: false
schema:
type: string

schemas:
EnterpriseDetailResponse:
title: Enterprise Detail Response
type: object
properties:
id:
type: string
description: Unique ID for this enterprise.
example: "3121"
mfa_enforced:
type: boolean
description: Indicates whether MFA is enforced for this enterprise.
example: true
modules:
type: array
description: List of modules this enterprise is authorized to access.
items:
type: string
example: [
human_resources,
project_management
]
name:
type: string
description: The name of the enterprise.
example: Kathy's Plumbing
status:
type: string
description: Inidicates current status of this enterprise.
enum:
- offline
- online
- pending_deletion
- unknown
example: online
@@ -0,0 +1,92 @@
openapi: '3.1.0'

info:
title: Our API
description: extended description of Our API
version: 0.1.0

servers:
- url: http://localhost
description: included for completeness; not our actual url

tags:
- name: Enterprise
description: Operations with Enterprise ID

paths:
/v0/enterprise/{id}:
get:
tags:
- Enterprise
summary: Get detailed info about an enterprise.
description: |
Returns detailed information for the specified Enterprise ID.
This operation may *only* be performed by a **platform admin** or a
client with sufficient permissions.
operationId: get_enterprise_detail
parameters:
- $ref: '#/components/parameters/traceIDHeader'
- $ref: '#/components/parameters/enterpriseIDPath'
responses:
404:
description: No enterprise matching the requested ID could be found.
content:
application/json: {}
components:
parameters:
enterpriseIDPath:
$ref: '#/components/parameters/idInPath'
description: The Enterprise ID used to perform this request.
example: "12422"

idInPath:
name: id
in: path
required: true
schema:
type: string

traceIDHeader:
name: X-Trace-Id
in: header
description: Optional UUID for log tracing
required: false
schema:
type: string

schemas:
EnterpriseDetailResponse:
title: Enterprise Detail Response
type: object
properties:
id:
type: string
description: Unique ID for this enterprise.
example: "3121"
mfa_enforced:
type: boolean
description: Indicates whether MFA is enforced for this enterprise.
example: true
modules:
type: array
description: List of modules this enterprise is authorized to access.
items:
type: string
example: [
human_resources,
project_management
]
name:
type: string
description: The name of the enterprise.
example: Kathy's Plumbing
status:
type: string
description: Inidicates current status of this enterprise.
enum:
- offline
- online
- pending_deletion
- unknown
example: online

0 comments on commit e2be707

Please sign in to comment.