Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/core/components/parameters/parameters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export default class Parameters extends Component {

const isExecute = tryItOutEnabled && allowTryItOut
const isOAS3 = specSelectors.isOAS3()
const isCallbackBody = specPath.some((val) => val === "callbacks")

const regionId = createHtmlReadyId(`${pathMethod[1]}${pathMethod[0]}_requests`)
const controlId = `${regionId}_select`
Expand Down Expand Up @@ -243,6 +244,7 @@ export default class Parameters extends Component {
requestBodyInclusionSetting={oas3Selectors.requestBodyInclusionSetting(...pathMethod)}
requestBodyErrors={oas3Selectors.requestBodyErrors(...pathMethod)}
isExecute={isExecute}
isCallback={isCallbackBody}
getConfigs={getConfigs}
activeExamplesKey={oas3Selectors.activeExamplesMember(
...pathMethod,
Expand Down
27 changes: 18 additions & 9 deletions src/core/plugins/oas3/components/request-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Map, OrderedMap, List, fromJS } from "immutable"
import { getCommonExtensions, stringify, isEmptyValue } from "core/utils"
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"

export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExamplesKey, fn) => {
export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExamplesKey, fn, isCallback = false) => {
const mediaTypeValue = requestBody.getIn(["content", mediaType]) ?? OrderedMap()
const schema = mediaTypeValue.get("schema", OrderedMap())

Expand All @@ -19,12 +19,14 @@ export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExample
])
: exampleSchema

const sampleGenConfig = isCallback
? { includeReadOnly: true }
: { includeWriteOnly: true }

const exampleValue = fn.getSampleSchema(
schema,
mediaType,
{
includeWriteOnly: true
},
sampleGenConfig,
mediaTypeExample
)
return stringify(exampleValue)
Expand All @@ -44,6 +46,7 @@ const RequestBody = ({
fn,
contentType,
isExecute,
isCallback = false,
specPath,
onChange,
onChangeIncludeEmpty,
Expand Down Expand Up @@ -93,6 +96,7 @@ const RequestBody = ({
contentType,
key,
fn,
isCallback,
), val)
}
return container
Expand Down Expand Up @@ -150,7 +154,8 @@ const RequestBody = ({
<tbody>
{
Map.isMap(bodyProperties) && bodyProperties.entrySeq().map(([key, schema]) => {
if (schema.get("readOnly")) return
if (!isCallback && schema.get("readOnly")) return
if (isCallback && schema.get("writeOnly")) return

const oneOf = schema.get("oneOf")?.get(0)?.toJS()
const anyOf = schema.get("anyOf")?.get(0)?.toJS()
Expand All @@ -167,9 +172,10 @@ const RequestBody = ({
const currentErrors = requestBodyValue.getIn([key, "errors"]) || requestBodyErrors
const included = requestBodyInclusionSetting.get(key) || false

let initialValue = fn.getSampleSchema(schema, false, {
includeWriteOnly: true
})
let initialValue = fn.getSampleSchema(schema, false, isCallback
? { includeReadOnly: true }
: { includeWriteOnly: true }
)

if (initialValue === false) {
initialValue = "false"
Expand Down Expand Up @@ -256,6 +262,7 @@ const RequestBody = ({
contentType,
activeExamplesKey,
fn,
isCallback,
)
let language = null
let testValueForJson = getKnownSyntaxHighlighterLanguage(sampleRequestBody)
Expand Down Expand Up @@ -300,7 +307,8 @@ const RequestBody = ({
schema={mediaTypeValue.get("schema")}
specPath={specPath.push("content", contentType, "schema")}
example={example}
includeWriteOnly={true}
includeReadOnly={isCallback}
includeWriteOnly={!isCallback}
/>
{
sampleForMediaType ? (
Expand All @@ -326,6 +334,7 @@ RequestBody.propTypes = {
specSelectors: PropTypes.object.isRequired,
contentType: PropTypes.string,
isExecute: PropTypes.bool.isRequired,
isCallback: PropTypes.bool,
onChange: PropTypes.func.isRequired,
onChangeIncludeEmpty: PropTypes.func.isRequired,
specPath: PropTypes.array.isRequired,
Expand Down
90 changes: 90 additions & 0 deletions test/unit/core/plugins/oas3/request-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { OrderedMap, fromJS } from "immutable"
import { getDefaultRequestBodyValue } from "core/plugins/oas3/components/request-body"

describe("getDefaultRequestBodyValue", () => {
const mockFn = {
getSampleSchema: jest.fn((schema, mediaType, config) => {
const result = {}
const schemaJS = schema && typeof schema.toJS === "function" ? schema.toJS() : schema
if (schemaJS && schemaJS.properties) {
Object.entries(schemaJS.properties).forEach(([key, prop]) => {
if (prop.readOnly && !config.includeReadOnly) return
if (prop.writeOnly && !config.includeWriteOnly) return
result[key] = prop.type === "string" ? "string" : prop.type
})
}
return result
}),
}

const buildRequestBody = (schema) =>
fromJS({
content: {
"application/json": {
schema,
},
},
})

const userSchema = {
type: "object",
properties: {
id: { type: "string", readOnly: true },
name: { type: "string" },
password: { type: "string", writeOnly: true },
},
}

afterEach(() => {
jest.clearAllMocks()
})

it("should include writeOnly and exclude readOnly for regular request bodies", () => {
const requestBody = buildRequestBody(userSchema)

getDefaultRequestBodyValue(
requestBody,
"application/json",
"default",
mockFn,
false
)

expect(mockFn.getSampleSchema).toHaveBeenCalledTimes(1)
const config = mockFn.getSampleSchema.mock.calls[0][2]
expect(config).toEqual({ includeWriteOnly: true })
expect(config.includeReadOnly).toBeUndefined()
})

it("should include readOnly and exclude writeOnly for callback request bodies", () => {
const requestBody = buildRequestBody(userSchema)

getDefaultRequestBodyValue(
requestBody,
"application/json",
"default",
mockFn,
true
)

expect(mockFn.getSampleSchema).toHaveBeenCalledTimes(1)
const config = mockFn.getSampleSchema.mock.calls[0][2]
expect(config).toEqual({ includeReadOnly: true })
expect(config.includeWriteOnly).toBeUndefined()
})

it("should default to non-callback behavior when isCallback is not provided", () => {
const requestBody = buildRequestBody(userSchema)

getDefaultRequestBodyValue(
requestBody,
"application/json",
"default",
mockFn
)

expect(mockFn.getSampleSchema).toHaveBeenCalledTimes(1)
const config = mockFn.getSampleSchema.mock.calls[0][2]
expect(config).toEqual({ includeWriteOnly: true })
})
})