how to get a mocked http response from swagger.yaml in a maven project - swagger

I am aware that swagger core , swagger inflector read a swagger.yaml / json file in creating a http response, Now we have a use wire mock to stub response where the content for the stub has to come from the swagger file.
I have gone through many examples but unable to figure out the right function to convert the swagger.yaml/json to return the http response. Please help me

SwaggerHub can be used to define your API in JSON or YAML. The UI provided alongside shows the API vividly. The UI has the option to trigger a newly defined API and check the response as a model - the structure of JSON response body. If "example" has been given in the API specification, it's fetched in the response.
If this response model works as per requirement, it can be then used in WireMock to generate stubbed responses having canned data. A swagger specification fragment and response model shown below -
API specification -
"swagger": "2.0",
"info":{
"version":"v0.1",
"title":
"Capital city finder",
"description":"Search capital city by country name"
},
"definitions":{
"city":{
"properties":{
"countryName":{
"type":"string",
"example":"United Kingdom"
},
"capitalCity":{
"type":"string",
"example":"London"
},
"nationalAnimal":{
"type":"string",
"example":"Lion"
},
"popularFood":{
"type":"string",
"example":"Fish & Chips"
}
... ... ...
Response Model -
[
{
"countryName": "United Kingdom",
"capitalCity": "London",
"nationalAnimal": "Lion",
"popularFood": "Fish & Chips"
}
]
Not sure if this is still relevant to you now.

Related

Mock JSON data parsing in GraphQL Apollo Client iOS

I am returning MOCK JSON response for GraphQL query response in Apollo iOS client. But Apollo client is not parsing the MOCK json content.
Error: __typename missing in mock response.
I found the solution after spending some time on Apollo Client framework iOS.
Mock JSON response must contains __typename key-value in each structure inside it. So that Apollo client decoding functions can decide the corresponding struct models for the response.
{
"data": {
"summary": {
"__typename": "AccountSummaryResponse"
"accountSummary": [{
"token": "45454"
"type": "club"
"__typename": "AccountSummary"
},
{
"token": "4344"
"type": "clubA"
"__typename": "AccountSummary"
}
]
}
}
}
You will get the __typename value from API.swift automated generated file by Apollo Code-gen.

Swagger: query params on POST methods not allowed?

I've got a POST endpoint described in Swagger and I want that endpoint to also have query parameters. We're using 1.2 swagger format because, well, legacy reasons. We use 3scale, it hosts the documents and you edit your swagger in their web UI. However, when I try to save the document it gives me the following error.
JSON Spec can not have paramType='body' and paramType='query' on the same method
I can't find anything in the swagger specs that says this is an actual limitation. Is this likely something specific to 3Scale or is this a general swagger limitation? And if the latter, can someone point me at a spec is that clarifies it?
The actual REST endpoint doesn't care, it's happy with query params on a POST. It's just getting the Swagger tool to be happy. Here's the abbreviated snippet of the swagger doc:
{
"parameters": [
{
"name": "myQueryParam",
"dataType": "string",
"paramType": "query",
"required": true
},
{
"name": "body",
"dataType": "string",
"paramType": "body",
"required": true
}
],
"httpMethod": "POST"
}
not sure if the error message is a generic validation error, but there a couple of error in the specification you shared:
it is "method" and not "httpMethod"
it is "type" and not "dataType"
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/1.2.md
The following example works for me, but I use required=false:
{
"in": "query",
"name": "myQueryParam",
"required": false,
"type": "string"
}
See also Swagger parameters in query and/or body

Response Templating for a Static Json File

I am using WireMock in java to stub for a POST request. The request returns a json body file that is stored in my local. The stub looks like this:
wireMockServer.stubFor(get(urlMatching("/v1/invoices/.*"))
.willReturn(aResponse()
.withStatus(200)
.withBodyFile("testgetupgradeprorationamount/stubThree")));
Part of the response body file, "stubThree" looks like this:
"id": "ii_1EmM93Htp4Kkdrs8",
"object": "line_item",
"amount": 9600,
"currency": "usd",
"description": "Remaining time on 3 × Business after 17 Jun 2019",
"discountable": false,
"invoice_item": "ii_1EmM93HtpLyYzpmOC4Kkdrs8",
"livemode": false,
"metadata": {
},
"period": {
"end": 1563374954,
"start": 1560782957
}
The request url has a number of paramters and looks like this:
/v1/invoices/?subscription_items[0][quantity]=3&subscription_proration_date=1560892137&customer=cus_FHNIIE4b8LH7qL"
The stubbing works fine, but my goal is to give a dynamic response using response templating. I want to update the "start" field of the json file only, using the "subscription_proration_date" value from the request url.
I changed the start field and the stub like this:
"period": {
"end": 1566053354,
"start": "{{request.query.subscription_proration_date}}"
},
wireMockServer.stubFor(get(urlMatching("/v1/invoices/.*"))
.willReturn(aResponse()
.withStatus(200)
.withBodyFile("testgetupgradeprorationamount/stubThree")
.withTransformers("response-template")));
This is not working for me, so any directions would be really helpful. I also tried removing the quotations around the start field handlebar in the file, and that did not work either.
Thank you in advance!
so I was able to resolve my issue. The problem was that I did not add the proper extension to my WireMockServer instance:
.extensions(new ResponseTemplateTransformer(false));
If the boolean value is false, you additionally need to specify the transformer on a per stub basis like I did above. Otherwise, the extension is applied to all stubs.

Azure Logic App returns 404 with valid Project Online ODATA query using /_api/Projectdata

I want to filter and select Project Online data using Enterprise Custom Fields (ECF) in an Azure Logic App. I start with the out-of-the-box Project Online connector to trigger on project publish, then list project and task details.
Using the default connector, I can return project data but it does not contain the ECF data. Looking at the generated code, the Project Online connector uses /_api/ProjectServer. If I make similar requests (outside of Logic Apps) using /_api/Projectdata I get the ECF data.
If I edit the Logic App code with modified URL and fields, I get a 404 error. There seems to be something missing that I cannot see when I make the request with the modified values from within the Logic App.
My guess is the connector is limiting access to /_api/Projectdata (though the trigger itself is using this URL).
The following returns the data I want if I request it straight from the browser:
https://tenant.sharepoint.com/sites/PWA/_api/Projectdata/Projects(guid'project_GUID')
This is the raw input in the logic app:
{
"host": {
"connection": {
"name": "/subscriptions/subscription_GUID/resourceGroups/MyRG/providers/Microsoft.Web/connections/projectonline-1"
}
},
"method": "get",
"path": "/_api/Projectdata/Projects(guid'project_GUID')",
"queries": {
"siteUrl": "https://tenant.sharepoint.com/sites/PWA"
}
}
This is the raw output of the error:
{
"statusCode": 404,
"headers": {
"Access-Control-Allow-Methods": "GET, PUT, PATCH, DELETE, POST",
"Access-Control-Allow-Origin": "*",
"Access-Control-Max-Age": "3600",
"Access-Control-Expose-Headers": "*",
"Date": "Thu, 20 Sep 2018 16:58:42 GMT",
"Content-Length": "54",
"Content-Type": "application/json"
},
"body": {
"statusCode": 404,
"message": "Resource not found"
}
}
URLs and GUID have been modified in the examples above.

Simple swagger specification, to retrieve an html web page

I'm new to swagger, and I'm trying to make a very simple specification, with only a get method in order to retrieve a web page, this is the code:
{
"swagger": "2.0",
"info": {
"title": "example",
"description": "Sample api to retrieve a web page.",
"version": "0.1"
},
"host":"example.org", #"localhost:8080",
"schemes": [
"https"
],
"paths": {
"/":{
"get":{
"summary":"Return the web page.",
"description":"",
"produces":["text/html"],
"responses":{
"200":{
"description":"OK",
},
"400":{
"description":"Bad request"
},
"404":{
"description":"Not Found"
}
}
}
}
}
}
I'm using the swagger online editor.
Unfortunately when I execute the request, it did not return the web page and no one of the status code that I have implemented in the specification, it return me in the detail section the error:
TypeError: Failed to fetch
Someone can tell me where I'm wrong?
Thank you.
For the "Try it out" button to work in the Swagger online editor, your API endpoints must be CORS-enabled. That is, your server (example.org or localhost:8080) must be configured to return certain response headers that would allow editor.swagger.io to make cross-domain requests to your server. This is explained in more details here:
https://github.com/swagger-api/swagger-ui#cors-support
The way to configure CORS depends on the server/framework used to host the app. This page has instructions for some common web servers:
https://enable-cors.org/server.html

Resources