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

Multiple Joi.object() with className break docs #95

Closed
grabbou opened this issue Jan 20, 2015 · 3 comments
Closed

Multiple Joi.object() with className break docs #95

grabbou opened this issue Jan 20, 2015 · 3 comments

Comments

@grabbou
Copy link

grabbou commented Jan 20, 2015

If I generate schema:

var schema = Joi.object({
   _id: Joi.any().required(),
   name: Joi.string().required()
}).options({
   className: 'User'
});

that I include it let's say in response schema and then - I modify it like below:

var schema2 = schema.keys({
    name: Joi.string().optional()
});

or

var schema2 = schema.optionalKeys('name')

Swagger UI shows first schema (schema1) everywhere instead of displaying schema2 in request parameters.

After removing className, everything works as expected. This might be a bug due to className resolving.

@glennjones
Copy link
Collaborator

I am not going to be able to look at this for next three weeks as just about to leave for hols. If you want track down the issue and create a PR great, otherwise I pick it up on my return.

@grabbou
Copy link
Author

grabbou commented Feb 12, 2015

Also - when multiple routes are defined like so:

  • events/{id} GET
  • events/{id} POST
  • events/{id} DELETE
    And no className is set and the automatic one is generated (eventsid_response) and if responses are different (for example DELETE response returns affected rows), they are mixed up and all of them are the same as the first one ever defined (in this case - GET response schema).

Quick fix can be done by modifying this line:

var responseProperty = internals.validatorToProperty(responseClassName || op.nickname + '_response',

to

var responseProperty = internals.validatorToProperty(responseClassName || op.nickname + '_' + route.method + '_response',

I guess it is because Swagger returns all of models under "models" property, so if the name is the same, they overwrite themselves.

So - the results are:
eventsid_GET_response
eventsid_POST_response
eventsid_DELETE_response

Also I think that "name generation" should be done inside __getClassName, instead of doing || conditional in method args.

Can do PR for this and above as well if you want.

glennjones added a commit that referenced this issue Feb 14, 2015
@glennjones
Copy link
Collaborator

Hi Mike, sorry taken I while to get back to you

I believe I have fixed the two use case issues you where having. Can you try out v0.6.3 and make sure it works the way you expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants