Is there a tool that lets me generate a Swagger yaml definition model from sample JSON?
I would go in and and edit and clean up the yaml, but would be nice if there was something out there that would stub out the structure of a yaml based on a large or complex sample json object.
You can use Apigee's API Studio that has this feature:
https://apistudios.io
Click on Insert menu and then New Model. Form a JSON example you can create all of the paths and operations you want.
Related
I automatically generate schema for my classes using Json.NET Schema and it works great for validation. I wanted to add the ability for my classes to provide snippets directly and emit them into a (presumably separate) schema file, for use in Visual Studio Code.
https://code.visualstudio.com/docs/languages/json#_define-snippets-in-json-schemas
This is a non-standard schema extension of course. From the source code of Json.NET Schema, it looks like I cannot override the class that is used to serialize the schema to JSON, so I can't figure out how to make this work.
The only approach I have considered is to emit the schema, read it back as dumb JSON and then edit in the snippets. The resulting file could be a .vsschema or something like that and presumably no longer valid in the schema validator, so I would store it next to the "clean" one. Is there any nice way to do this that doesn't require me to path through the resulting JSON and make edits that way?
How to generate dynamic PactDslJsonBody using json value?
Is it possible Pact team can provide the auto builder to assign body dynamically?
Pact Body:
body(new PactDslJsonBody()
.object("testsuite")
.stringType("webId","24255")
.closeObject());
Assert Response:
"{\"testsuite\":{\"webId\":\"24255\"}}";
Based on Assert Response(as input) and create the dslbody like
String json = "{\"testsuite\":{\"webId\":\"24255\"}}"
//body(json);
body(generatePactDSLJsonBody(json));
Assert Response:
assertEqual("{\"testsuite\":{\"webId\":\"24255\"}}",json);
I know in body we can provide json itself. but i need to generate the PactDSLJson body using Json.
It is technically possible to auto-generate the DSL classes from a JSON document, but I do not see the benefit of your example. Most of the time the matchers are defined based on the semantics of the JSON payload, not the syntax.
For example, from your sample JSON, it would see the webId attribute as a string, and generate a string type matcher. However, it is clearly a number, not a string.
The auto-generated DSL body would accept the following JSON:
{"testsuite":{"webId":"This is not a web ID &^*&^%"}}
However, an auto-generation tool used to create a skeleton consumer test from a JSON document which could then be changed based on the semantics of the JSON would be really useful.
We build a library to generate the PactDslJsonBody from a Java Bean. That's not directly your use-case since you want to use JSON as input, but maybe you designed your endpoints to expose Java Beans, so you can use them for your Pacts.
You might want to have a look at https://github.com/remondis-it/pact-consumer-builder.
With this library you're able to define PactDslJsonBody mappings on a per-field or a per-type basis. In our case this reduces the boilerplate code to nearly a one-liner:
PactDslJsonBody jsonBody = ConsumerExpects.type(YOUR_BEAN_TYPE.class)
.useTypeMapping(...)
// Other field or type configurations
.build(new PactDslJsonBody(), YOUR_BEAN_SAMPLE_INSTANCE);
This performs the necessary calls on the PactDslJsonBody and you can use the result for your Pact test.
Btw: The Pact Consumer Builder library works well in conjunction with a fixture generator that produces test data instances for you Java Beans. You can use our fixture generator (https://github.com/remondis-it/resample) but every other Java Bean instance generator should work, too.
I'm trying to come up with a reusable pattern for updating MongoDB Documents when using Spring Data in conjunction with Spring MVC.
The use case can generally be summarized by:
A document is created in Mongo using repository.save()
Parts of that document are then presented in a Spring MVC editable form.
A user submits updated parts of that document which are then saved.
If I use the repository.save() method in step 3, I will lose any data in the document that was not bound to the form. Making the form responsible for the entire document is fragile so this is where it seems the findAndModify() method of the MongoTemplate comes in handy.
To use the findAndModify() method, I've created Form objects that support a toMap() method which takes the Form object's properties as a Map and removes some of the fields (e.g. class and id). This gets me a Map that contains only the fields that I care about from the Form object. Passing the object ID and this map to an update() method on my customized repository, I build Query and Update objects that I can pass to the findAndModify() method.
Using this approach, I'm able to add fields to my objects easily and only worry about instances when there are fields I don't want to update from a form posting. Document fields not manipulated by the Form should be retained. It still seems slightly convoluted to be using both the Repository and MongoTemplate so I'm wondering if there are better examples for how to handle this. It seems like this should be a consistent pattern when working with Mongo and Spring MVC (at the least).
I've created a sample project showing how I achieve this model on GitHub. The Spock Tests show how "updating" a Document using save() will blow away fields as expected and my update() method.
https://github.com/watchwithmike/diner-data
What are other people doing when dealing with partial updates to Documents using Spring MVC and Spring Data?
If you are taking whatever the user supplies and just shoving that in the database you are running the risk of doing something dangerous like updating or creating data that they shouldn't be able to. Instead, you should first query Mongo to get the most recent version of the document, change any fields (it looks like you are using Groovy so you could loop through all the properties and set them on the new document), and then save the new, complete document.
If you are making small, consistent updates (like increasing the number of votes, or something like that), you could create a custom MongoDB query using the MongoTemplate to do an update to a few fields. Check out the spring-data-mongodb docs for more. You can also add custom methods to the MongoRepository that use the MongoTemplate.
I have the following method signature for an action on an MVC controller:
public ActionResult DoSomething(int id, string anotherParameter, IEnumerable<StronglyTypedThing> data)
{
}
This method is called by an AJAX Request (in this instance I'm using ExtJS, but that should have little/no bearing on this I imagine!) which passes up, for example:
id: 1,
anotherParameter: 'cake',
data: '<stronglyTypedThings>
<stronglyTypedThing>
<id>1</id>
<anotherProperty>Smith, John></anotherProperty>
</stronglyTypedThing>
<stronglyTypedThing>
<id>2</id>
<anotherProperty>Doe, Jane></anotherProperty>
</stronglyTypedThing>
</stronglyTypedThings>'
Currently the method signature I've shown above is not what I have, instead the final parameter is defined as string data and I have what is effectively boilerplate code which transforms the XML string into an IEnumerable<StronglyTypedThing>.
Is there a way to have (either by virtue of something baked into MVC, or by extending it) MVC deal with the grunt-work for me so I don't have the boilerplate code present in my action method?
You can create a custom model binder.
This link will have an example of custom xml binder: http://lostechies.com/jimmybogard/2011/06/24/model-binding-xml-in-asp-net-mvc-3/
You might want to look into a custom ValueProviderFactory.
A custom XmlValueProviderFactory will parse the incoming xml string and construct an intermediate dictionary. (which MVC uses to model bind)
Based on your need, you could parse the whole XML/or a part of it, to construct the dictionary equivalent of your Model object. Once there, MVC will take care of creating the Model for you using Model Binding. Also, value providers have the additional benefit of input validation which custom model binders don't have.
Please see following help links to see a JSON & XML Value Provider factory.
i think the JSON Value provider is now in built, but not the XML one. not sure.
http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx/
http://www.nogginbox.co.uk/Media/files/XmlValueProviderFactory.txt
I'm writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I'd prefer not to generate the data in code like this:
var mockData = new Array();
mockData.push(new Foo(1, "abc", "xyz"));
mockData.push(new Foo(2, "def", "xyz"));
...
Rather I'd like to store the data in a file in some format that it can be easily serialized into my strongly-typed value objects (i.e. Foo above). Ideally I'd like to create the data in a self-describing format (i.e. what data type each field is, what class it represents, etc)
Does this make sense? Any suggestions?
I would highly recommend the asx3m library. It easily allows serialization to a very readable XML format like this for an object of class Foo:
<com.example.Foo>
<myVar>Something</myVar>
<myArrList>
<string>one</string>
<string>two</string>
</myArrList>
</com.example.Foo>
The code to de-serialize looks like this:
Asx3mer.instance.fromXML(someXMLObj)
The project site has some good examples and it's not too hard to get this off the ground.
Write a method to serialize an "inflated" version of your object. Put the output of that into a file and load it up as part of your test setup. When you want to edit the values, simply edit the xml file. I dont know if this is possible in flex but I will usually include these files as a resource in my test library so that I do not need to copy the file to any specific location for a test run.