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

added support for examples if no default. Also added support for properties when type is not an object #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion schema-instantiator.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ function instantiatePrimitive (val) {
return val.default
}

if (val.example) {
return val.example
}
Copy link
Contributor

@michaelgwelch michaelgwelch Jul 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend checking example before default.

See "Common Mistakes" at https://swagger.io/docs/specification/describing-parameters/

There are two common mistakes ...

  • Using default to specify a sample value. This is not intended use of default and can lead to unexpected behavior in some Swagger tools. Use the example or examples keyword for this purpose instead. See Adding Examples.

So while default might be a fine example if nothing else is available, I'd default to example first and then the first entry in examples and then finally default. That's what I did in #86 for parameter parsing.

return typesInstantiator[type]
}

Expand Down Expand Up @@ -137,7 +140,7 @@ function instantiate (schema, options) {
var i
var type = obj.type
// We want non-primitives objects (primitive === object w/o properties).
if (type === 'object' && obj.properties) {
if (obj && obj.properties) {
data[name] = data[name] || { }

// Visit each property.
Expand Down