Skip to content

Commit

Permalink
refactor(oas3): refactor getting initial values for request body in O…
Browse files Browse the repository at this point in the history
…penAPI 3.x
  • Loading branch information
glowcloud committed Mar 28, 2024
1 parent 8d1b140 commit c99fee1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
55 changes: 29 additions & 26 deletions src/core/plugins/oas3/components/request-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,46 +150,49 @@ const RequestBody = ({
<table>
<tbody>
{
Map.isMap(bodyProperties) && bodyProperties.entrySeq().map(([key, prop]) => {
if (prop.get("readOnly")) return
Map.isMap(bodyProperties) && bodyProperties.entrySeq().map(([key, schema]) => {
if (schema.get("readOnly")) return

const oneOf = prop.get("oneOf")
const anyOf = prop.get("anyOf")
const oneOf = schema.get("oneOf")
const anyOf = schema.get("anyOf")

if (oneOf || anyOf) {
prop = oneOf ? oneOf.get(0) : anyOf.get(0)
const nestedSchema = List.isList(oneOf)
? oneOf.get(0)
: List.isList(anyOf)
? anyOf.get(0)
: null

if (Map.isMap(nestedSchema)) {
schema = nestedSchema
}
}

let commonExt = showCommonExtensions ? getCommonExtensions(prop) : null
let commonExt = showCommonExtensions ? getCommonExtensions(schema) : null
const required = schemaForMediaType.get("required", List()).includes(key)
const type = prop.get("type")
const format = prop.get("format")
const description = prop.get("description")
const type = schema.get("type")
const format = schema.get("format")
const description = schema.get("description")
const currentValue = requestBodyValue.getIn([key, "value"])
const currentErrors = requestBodyValue.getIn([key, "errors"]) || requestBodyErrors
const included = requestBodyInclusionSetting.get(key) || false

const useInitialValFromSchemaSamples = prop.has("default")
|| prop.has("example")
|| prop.hasIn(["items", "example"])
|| prop.hasIn(["items", "default"])
const useInitialValFromEnum = prop.has("enum") && (prop.get("enum").size === 1 || required)
const useInitialValue = useInitialValFromSchemaSamples || useInitialValFromEnum

let initialValue = ""
if (type === "array" && !useInitialValue) {
initialValue = []
let initialValue = fn.getSampleSchema(schema, false, {
includeWriteOnly: true
})

if (initialValue === false) {
initialValue = "false"
}
if (type === "object" || useInitialValue) {
// TODO: what about example or examples from requestBody could be passed as exampleOverride
initialValue = fn.getSampleSchema(prop, false, {
includeWriteOnly: true
})

if (initialValue === 0) {
initialValue = "0"
}

if (typeof initialValue !== "string" && type === "object") {
initialValue = stringify(initialValue)
}

if (typeof initialValue === "string" && type === "array") {
initialValue = JSON.parse(initialValue)
}
Expand All @@ -208,7 +211,7 @@ const RequestBody = ({
{!showCommonExtensions || !commonExt.size ? null : commonExt.entrySeq().map(([key, v]) => <ParameterExt key={`${key}-${v}`} xKey={key} xVal={v} />)}
</div>
<div className="parameter__deprecated">
{ prop.get("deprecated") ? "deprecated": null }
{ schema.get("deprecated") ? "deprecated": null }
</div>
</td>
<td className="parameters-col_description">
Expand All @@ -217,7 +220,7 @@ const RequestBody = ({
<JsonSchemaForm
fn={fn}
dispatchInitialValue={!isFile}
schema={prop}
schema={schema}
description={key}
getComponent={getComponent}
value={currentValue === undefined ? initialValue : currentValue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ describe("OpenAPI 3.0 Allow Empty Values in Request Body", () => {
.get(".try-out__btn")
.click()
// Request Body
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .json-schema-form-item-remove")
.click()
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .parameter__empty_value_toggle input")
.should("be.checked")
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(6) > .parameters-col_description select")
.select("--")
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(6) > .parameters-col_description .parameter__empty_value_toggle input")
.should("be.checked")
})
Expand All @@ -49,6 +53,8 @@ describe("OpenAPI 3.0 Allow Empty Values in Request Body", () => {
.get(".try-out__btn")
.click()
// Request Body
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .json-schema-form-item-remove")
.click()
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .parameter__empty_value_toggle input")
.should("be.checked")
.uncheck()
Expand All @@ -67,9 +73,11 @@ describe("OpenAPI 3.0 Allow Empty Values in Request Body", () => {
// Expand Try It Out
.get(".try-out__btn")
.click()
// add item to pass required validation
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description button")
// Remove example values
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .json-schema-form-item-remove")
.click()
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(6) > .parameters-col_description select")
.select("--")
// Execute
.get(".execute.opblock-control__btn")
.click()
Expand All @@ -92,11 +100,11 @@ describe("OpenAPI 3.0 Allow Empty Values in Request Body", () => {
.get(".try-out__btn")
.click()
// Request Body
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .json-schema-form-item-remove")
.click()
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .parameter__empty_value_toggle input")
.uncheck()
// add item to pass required validation
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description button")
.click()
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) input")
.clear()
// Execute
Expand All @@ -122,15 +130,14 @@ describe("OpenAPI 3.0 Allow Empty Values in Request Body", () => {
.get(".try-out__btn")
.click()
// Request Body
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .json-schema-form-item-remove")
.click()
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .parameter__empty_value_toggle input")
.uncheck()
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(6) > .parameters-col_description select")
.select("--")
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(6) > .parameters-col_description .parameter__empty_value_toggle input")
.uncheck()
// add item to pass required validation
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description button")
.click()
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) input")
.clear()
// Execute
.get(".execute.opblock-control__btn")
.click()
Expand Down
15 changes: 1 addition & 14 deletions test/e2e-cypress/e2e/features/oas3-request-body-required.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe("OpenAPI 3.0 Validation for Required Request Body and Request Body Fiel
.get(
".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(1) > .parameters-col_description input"
)
.clear()
.should("not.have.class", "invalid")
// Execute
.get(".execute.opblock-control__btn")
Expand Down Expand Up @@ -163,11 +164,6 @@ describe("OpenAPI 3.0 Validation for Required Request Body and Request Body Fiel
// Expand Try It Out
.get(".try-out__btn")
.click()
// add item to get input
.get(
".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description button"
)
.click()
// Execute
.get(".execute.opblock-control__btn")
.click()
Expand Down Expand Up @@ -217,15 +213,6 @@ describe("OpenAPI 3.0 Validation for Required Request Body and Request Body Fiel
".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(2) > .parameters-col_description input"
)
.should("not.have.class", "invalid")
// add item to get input, just an extra confirmation of non-invalid class
.get(
".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description button"
)
.click()
.get(
".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description input"
)
.should("not.have.class", "invalid")
})
it("after application/x-www-form-urlencoded 'invalid' error, on switch content type to application/json, SHOULD be free of errors", () => {
cy.visit("/?url=/documents/features/petstore-only-pet.openapi.yaml")
Expand Down

0 comments on commit c99fee1

Please sign in to comment.