Skip to content

fix(oas3): get examples for oneOf and anyOf in request body #9754

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

Closed
wants to merge 1 commit into from

Conversation

glowcloud
Copy link
Contributor

Refs #9745

@glowcloud
Copy link
Contributor Author

The current code for getting initial values for request body and for content types application/x-www-form-urlencoded and multipart/... is there to replicate the behaviour of forms for parameters in query, header, etc.

There, if we don't have an example, we won't be generating one, unless the type of the parameter is an object. We can see this in the screenshot, where the only parameter that has an example defined in the schema is the username:

Screenshot 2024-03-27 at 15 10 07

To replicate this behaviour, we only get the initial value if we have an object parameter or an example in the schema:

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

We need an additional check for arrays with examples, as getSampleSchema would return ex. '["test"]', so we need to parse it to ["test"], here:

if (typeof initialValue === "string" && type === "array") {
initialValue = JSON.parse(initialValue)
}

For objects, I think that this is there in case if getSampleSchema doesn't return a string for an object parameter:

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

If we want to move to use getSampleSchema directly, we can do it, but I think these checks for array and object would still need to be there. We would also need to add checks for anyOf and oneOf because we won't get these values for them otherwise:

const type = prop.get("type")
const format = prop.get("format")
const description = prop.get("description")

The behaviour between the form for parameter in query etc. and for request body would be different. On the other hand, we would also align the initial value with the value we get for application/json and application/xml, and from resetting the form.

@glowcloud
Copy link
Contributor Author

I noticed an issue with falsy values that was there before. If we have false or 0 as an example, we don't fill the integer parameter with 0 and both of the parameters will have the Send empty value option checked:

Screenshot 2024-03-27 at 15 48 28

Despite that, we would send these values anyway:

Screenshot 2024-03-27 at 15 48 56

@glowcloud
Copy link
Contributor Author

We would also need to add checks for anyOf and oneOf because we won't get these values for them otherwise:

const type = prop.get("type")
const format = prop.get("format")
const description = prop.get("description")

Only setting these values isn't enough, as we pass prop to JsonSchemaForm:

<JsonSchemaForm
fn={fn}
dispatchInitialValue={!isFile}
schema={prop}
description={key}
getComponent={getComponent}
value={currentValue === undefined ? initialValue : currentValue}
required = { required }
errors = { currentErrors }
onChange={(value) => {
onChange(value, [key])
}}
/>

Example usage in JsonSchemaForm:

const enumValue = schema && schema.get ? schema.get("enum") : null
const format = schema && schema.get ? schema.get("format") : null
const type = schema && schema.get ? schema.get("type") : null
const schemaIn = schema && schema.get ? schema.get("in") : null

It looks like we would need to set anyOf/oneOf schemas as prop anyway.

@glowcloud
Copy link
Contributor Author

Superseded by #9762

@glowcloud glowcloud closed this Mar 29, 2024
@glowcloud glowcloud deleted the issue-9745 branch March 29, 2024 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant