JamesMessinger / swagger-suite

No longer maintained. Please use https://github.com/swagger-api/swagger-node
MIT License
52 stars 34 forks source link

What is the best approach for bootstrapping mock data #4

Open deardooley opened 9 years ago

deardooley commented 9 years ago

First off, love the project. I was sitting down to take a crack at something like this when I ran across your project. Thank you.

Second, I'm having trouble using the mock server with a readonly API. I thought the way it would work was the routes would be picked by automagically and data from the swagger model "example" object would be returned. After digging through the code, I see you explicitly register mock data in a file you load from disk. That's fine, but it breaks consistency with the swagger definition and means I need to manually code a server.js file for every new API. Ideally, I'd like to be able to write my spec and kick the API without having to make the spec authors code. Do you have thoughts on the best way to approach this? Some thoughts I had were:

I realize these are not general problems, but is does speak to the usability of the mock server early on when developing an API. I'm happy to contribute and issue a pull request. I'd like to hear your ideas on how to approach this problem.

JamesMessinger commented 9 years ago

Thanks for the great feedback. I'm currently working on the 1.0 release of Swagger Suite, which will have lots of new features, bug fixes, and a whole new way of loading, defining, and managing mock data.

The golden rule that I follow is that the Swagger spec is always my first resort. Developers shouldn't have to write any code to get a functional mock API. Even in the current version of Swagger Suite, it will use the default value if you specify one in your Swagger spec. If you want to get really creative, you can have the default point to an external JSON file via a $ref pointer. Have you tried doing that?

I realize that Swagger-Suite is very poorly documented, so features like this aren't always very intuitive. Documentation is the next priority on my list once I get version 1.0 released.

deardooley commented 9 years ago

I completely understand. I saw you were the lone contributor and realized that I’d be digging through source to track stuff down. Even so, your inline documentation is much better than I expected. I’ll check out the default value support.

In your user-manager sample, the user.yaml file has an example stanza:

example:
  username: jdoe
  name:
    firstName: John
    lastName: Doe
  email: john.doe@abc.com
  telephone: 5551234567
  lastLoginDate: 2015-05-11T14-00-32Z

Did I miss this in the new spec? Is this the replacement for the default value in the 1.2 spec?

JamesMessinger commented 9 years ago

example is part of JSON schema. It's just so people who are reading the JSON schema can see an example of what a valid model would look like.

default is also part of JSON schema. It's purpose is similar to example (and, to eliminate confusion, I probably should have used default instead of example in my samples). The difference between default and example is that default is actually intended to instruct tools (such as Swagger Suite) to provide use the default value if no other value is available, whereas example is purely for informational purposes only.

JamesMessinger commented 9 years ago

Just FYI - the new version of Swagger Suite is coming along nicely, but taking a lot longer than I originally expected. The entire middleware layer is done now - including the new mocks - and I've released it as a separate, standalone project (Swagger Express Middleware), complete with code samples and walkthroughs. Feel free to check it out and let me know what you think.

The new mock middleware supports default and example values, as we discussed. In addition, the API for loading your own mock data is now much easier to understand, and it's actually documented now!

deardooley commented 9 years ago

Good stuff, James. I pulled the middleware server down last night. I had to convert a few older API def from 1.2 to 2.0 which took longer than I wanted it to, but it was a good exercise to go through. Our primary API is much, much larger and the learning experience will help a ton. I’m looking forward to mocking our next round of APIs with the Express Middleware. Out of curiosity, how hard would it be to add a MongoDataStore? Can the server Mock auth or is that out of scope?

— Rion

On Mar 16, 2015, at 9:39 AM, James Messinger notifications@github.com wrote:

Just FYI - the new version of Swagger Suite is coming along nicely, but taking a lot longer than I originally expected. The entire middleware layer is done now - including the new mocks - and I've released it as a separate, standalone project (Swagger Express Middleware https://www.npmjs.com/package/swagger-express-middleware), complete with code samples and walkthroughs. Feel free to check it out and let me know what you think.

The new mock middleware supports default and example values, as we discussed. In addition, the API for loading your own mock data is now much easier to understand, and it's actually documented now! https://github.com/BigstickCarpet/swagger-express-middleware/blob/master/docs/exports/DataStore.md — Reply to this email directly or view it on GitHub https://github.com/BigstickCarpet/swagger-suite/issues/4#issuecomment-81704696.

JamesMessinger commented 9 years ago

Glad to hear you're trying out the new middleware. I'm still working on the next versions of Swagger Server and Swagger Suite, which will both use the new middleware as well. Please let me know if you have any problems or find any bugs. Or better yet, open an issue! :)

I don't have any plans to write a MongoDataStore myself, but you can write your own by inheriting from the DataStore abstract class and implementing the __openDataStore and __saveDataStore methods. If you want to contribute it as a pull request, I'd be happy to accept it!

I do intend to implement mock auth, and I've added it to the to-do list.

myu-1987 commented 8 years ago

I am using swagger API for my application and i need a sample mock data for my application to bind a html control. So, is there any ways to insert mock data in API ?