Skip to content
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

Supporting JSON schema type array in examples and rendering #1052

Closed
pietermees opened this issue Dec 17, 2024 · 1 comment
Closed

Supporting JSON schema type array in examples and rendering #1052

pietermees opened this issue Dec 17, 2024 · 1 comment
Labels
enhancement New feature or request reviewing 👀 Undergoing manual audit to determine if issue should still be active roadmap Features or bug fixes planned for future release

Comments

@pietermees
Copy link

pietermees commented Dec 17, 2024

Opening this as an issue to document my findings. I might find time in the future to look at this and provide a PR, but not sure when that would happen.

Up until OpenAPI 3.0, nullable was an extension to JSON Schema.

Now that nullable is no longer valid in OpenAPI 3.1, the new proposed way to define a nullable field seems to be to use an array of values for the type.

Something like:

type:
  - string
  - "null"

This is aligned with the JSON Schema type spec. And is shown in the spec as an example.

I wasn't able to spend a lot of time on it yet, but it seems like this currently breaks the following code that is used to generate examples:

let fn = schema.default ? () => schema.default : primitives[type].default;
if (format !== undefined) {
fn = primitives[type][format] || fn;
}
if (fn) {
return fn(schema);
}
return "Unknown Type: " + schema.type;

It would be relatively easy to resolve this issue by detecting the array there and defaulting to the first entry for example.

However, it seems like there is some code elsewhere that does the $ref folding into the schema object, which is stringifying the type value into something like "string,null", so it's not properly handling the array case. I'm not sure where this code is located.

Possibly here?

type?: "string" | "number" | "integer" | "boolean" | "object" | "array";

We'd probably also need to update the nullable property rendering here to account for this new scenario?

@pietermees pietermees changed the title Supporting JSON schema type array Supporting JSON schema type array in examples and rendering Dec 17, 2024
@sserrata
Copy link
Member

Hi @pietermees, your proposed fix(es) and findings make sense. Currently, our plugin strictly supports the schema.nullable syntax, but it should be relatively easy to extend support to the array syntax. Feel free to open a PR, otherwise our team will try to tackle it in an upcoming release.

@sserrata sserrata added enhancement New feature or request reviewing 👀 Undergoing manual audit to determine if issue should still be active roadmap Features or bug fixes planned for future release labels Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request reviewing 👀 Undergoing manual audit to determine if issue should still be active roadmap Features or bug fixes planned for future release
Projects
None yet
Development

No branches or pull requests

2 participants