I'm currently working with ServiceStack's Swagger plugin and I'm having trouble populating certain objects in my request, specifically Dictionary objects.
In the image below, I want to pass a Dictionary<string,string> object as JSON for FieldFilter. What is the format for this? I've tried several examples but the FieldFilter object is always being deserialized as null.
UPDATE
I've taken a look at the JSON example provided in this link:
http://www.newtonsoft.com/json/help/html/SerializeDictionary.htm
which shows that a Dictionary<string,string> JSON representation is as follows:
{
"James": "9001",
"Jo": "3474",
"Jess": "11926"
}
but the result is still deserialized to null. I think this is more a limitation of ServiceStack and the Swagger plugin. I'll take on the suggestion of using a list of classes instead and provide another update
UPDATE 2
I've tried creating a FieldFilter class (which contains two string properties). This is the JSON I'm entering now:
{
"FieldId": "1",
"Value": "Hello"
},
{
"FieldId": "2",
"Value": "Goodbye"
}
Trying this (and other slight variations) the field is still being deserialized as null.
As a test, I added the field filter object to my CreateUser request, which has a single body with all the fields required. The JSON for that looks like this:
{
"FieldFilter": [
{
"FieldId": "1",
"Value": "Hello"
},
{
"FieldId": "2",
"Value": "Goodbye"
}
],
"FirstName": "",
"LastName": "",
"Password": "",
"Permissions": [
""
],
"Roles": [
""
],
"Username": ""
}
Now in this example where all the fields are in one JSON request, the FieldFilter field is deserialized with the correct values! Is there a reason why it works here? Am I missing something painfully obvious?
Related
I'm trying to include a bad request (400) response schemas in the open api document for the web api I'm building in .NET 6. The problem is, the media type always defaults to application/octet-stream even though I explicitly set it to application/json.
My controller is decorated with the following attribute:
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(BadRequestResult), (int)HttpStatusCode.BadRequest, "application/json")]
But, the media type for the response 400 is incorrect in the generated open api document:
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Foo"
}
}
}
},
"400": {
"description": "",
"content": {
"application/octet-stream": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
}
Am I missing something or is this a bug in NSwag?
So this is no bug, I was using ObjectResult as the response type instead of ProblemDetails. Apparently this class could not be serialized or something and that produced a different response media type.
So no bug!
I created a catalog api using asp.net core and cosmos db .I post a catalog data set. but i get following error.I debug it using break points the problem is my result body is null.
{"errors":{"id":["The value 'AddOrder' is not valid."]},"title":"One or more validation errors occurred.","status":400,"traceId":"80000011-0005-fe00-b63f-84710c7967bb"}
Catalog controller code example
Catalog repository code example
azure cosmos db structure :
What am i doing wrong ??
post json file :
{
"Id": 1,
"venderId": 1,
"IndustryName": "dsa",
"ProductName": "sad",
"CurrentQuantity": 1,
"Tag": "sad",
"Unit": "dsad",
"Price": 34,
"Class": "dsfs",
"category": {
"Id": 1,
"Name": "freg",
"Description": "fdf",
"subcategory": [
{
"Id": 1,
"Name": "ergt",
"Description": "erfwef"
}
]
}
}
Your controller code is validating the Model and returning the BadRequest: https://github.com/kajasumanie/calalogapi/blob/master/CalalogAPI/Controllers/BooksController.cs#L41
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
The error you are seeing is unrelated to Cosmos DB, whatever payload you are sending is not valid for the Catalog class.
That class is not available in your repo, so it's hard to say. You should check the payload versus the model class and verify the format and requirements are met.
I indicate the Id as Guid value. When try to add the order, I gave the id value as 1,2,3....
But Guid value I need to pass the Id value like 80000011-0005-fe00-b63f-84710c7967bb.
That is the problem .
For Example: I have a POST API with json request as below
{
"items":[
{
"name": "abc",
"gender": "female",
"companyName": "test"
}
]
}
Now, I would like to add a new field called comment to indicate what the json request is testing for? something like this
{
"comment": "test employee with name",
"items":[
{
"name": "abc",
"gender": "female",
"companyName": "test"
}
]
}
Is it a good practice to add/modify params for testing purpose to a json request?
If not, what are the other ways to do this?
I am using TestNG, RestAssured and JAVA to do API automation testing
This will not even work as the json schema for payload will be changed if you add another attribute. That will throw you an exception.
If you want to add description, you can add that to your tests. You can achieve this in testng by adding description attribute in the #Test annotation.
#Test(description = "This a new test", dependsOnMethods = "test1")
public void test2(){
...
}
in my Oracle db, i have a table namely, "remittanceHistories" which has a column called, "changelog" in which both request object sent and response object received are stored like this:
{
"req": {
"args": {
"chargingMode": "",
"destinationUri": "",
"cbsType": "",
"isBankDeposit": ""
},
"method": ""
},
"res": {
"code": "",
"message": "",
"transactionId": "18070685476",
"chTransactionId": "10111715789"
}
}
I want a query that finds "chTransactionId" only by skipping the rest.
Any help?
I have a java based service as the provider and a node JS app as the consumer.
I used a stub runner here https://github.com/spring-cloud-samples/stub-runner-boot for Node JS to run against the wiremock. But whether it's Node JS, browser or curl as client I get this "cursor" text in place of generated string from regex elements.
This is the contract:
request {
method GET()
url value(consumer(regex('/v2/accounts/[0-9]+')))
}
response {
status 200
headers {
contentType(applicationJson())
}
body (
"firstName": regex('[a-zA-Z]*'),
"lastName": regex('[a-zA-Z]*'),
"kycStatus": regex('FAILED|PASSED|PENDING|ERROR'),
"address": [
"streetAddress" : "3244 jackson street",
"city" : "City",
"state" : regex('[a-zA-Z]{2}'),
"zipcode": regex('^\\d{5}\$')
]
)
}
This is the actual response from wiremock:
Response:
HTTP/1.1 200
Content-Type: [application/json]
{
"firstName": {
"cursor": 9
},
"lastName": {
"cursor": 9
},
"kycStatus": {
"cursor": 27
},
"address": {
"streetAddress": "3244 jackson street",
"city": "City",
"state": {
"cursor": 11
},
"zipcode": {
"cursor": 7
}
}
}
I noticed that your cursor values are actually the number of characters in your regex. So that told me something was definitely wrong. I've never ran into this before.
I think you need to wrap your regex with value()
request {
method GET()
url value(consumer(regex('/v2/accounts/[0-9]+')))
}
response {
status 200
headers {
contentType(applicationJson())
}
body (
"firstName": value(producer(regex('[a-zA-Z]*'))),
"lastName": value(producer(regex('[a-zA-Z]*'))),
"kycStatus": value(producer(regex('FAILED|PASSED|PENDING|ERROR'))),
"address": [
"streetAddress" : "3244 jackson street",
"city" : "City",
"state" : value(producer(regex('[a-zA-Z]{2}'))),
"zipcode": value(producer(regex('^\\d{5}\$')))
]
)
}
I came across another example that affects request payloads in the same way, when generating wiremock stubs.
If I do not add at least one "field" to the request body, for example:
request{
// ...
body (
value(consumer(regex("[a-zA-Z0-9]+")), producer("derp"))
)
}
the request payload is required to be
{
"cursor" : 12
}
as seen by this wiremock stub generated in target/META-INF/.../mappings/myContract.json
"bodyPatterns" : [ {
"equalToJson" : "{\"cursor\":12}",
"ignoreArrayOrder" : false,
"ignoreExtraElements" : false
} ]
Solution
All I needed to do was add at least one field to the request body
body (
aMadeUpField: value(consumer(regex("[a-zA-Z0-9]+")), producer("derp"))
)
And the regex will now work for that field, as seen by my regenerated wiremock stub
"bodyPatterns" : [ {
"matchesJsonPath" : "$[?(#.['aMadeUpField'] =~ /[a-zA-Z0-9]+/)]"
} ]
This is probably why there is a line hidden in the documentation saying only JSON is supported for the request body.
Edit: Another thing to check is "anyBoolean()" which should be changed to "aBoolean()" and "anInteger()" which should be "anyInteger()" (SCC is i guess really inconsistent with naming...). I double check if they are right in IntelliJ by ctrl + hovering over the groovy methods and making sure they return DslProperty or ClientDslProperty
And, as the other poster said, be sure to wrap any regex() with value() and play around with consumer() and producer() if needed.