Related
I'm not very strong in swagger 2.0, could you please help me? I'm trying to describe body parameters, but got an error. Here is my swagger.json file:
{
"swagger": "2.0",
"info": {
"title": "Simple API overview",
"version": "v2"
},
"host": "localhost:4000",
"basePath": "/",
"paths": {
"/user/register": {
"post": {
"operationId": "register",
"summary": "User registration",
"parameters": [{
"in": "body",
"name": "role",
"required": true,
"schema": {
"type": "integer",
"example": 1
}
}]
}
}
}
}
When I try to run it, I got an error:
Error: Expected `string` for value, got `1`
If I remove example field, I got this in Example Value section:
{}
Looks like the type definition is incorrect, but I couldn't figure out what's the difference between my code and examples from swagger docs.
Any help is appreciated.
Thanks.
I found the reason: there should be consumes field defined as text/plain. By default it is application/json.
The full code:
{
"swagger": "2.0",
"info": {
"title": "Simple API overview",
"version": "v2"
},
"host": "localhost:4000",
"basePath": "/",
"paths": {
"/user/register": {
"post": {
"operationId": "register",
"summary": "User registration",
"consumes": ["text/plain"],
"parameters": [{
"in": "body",
"name": "role",
"required": true,
"schema": {
"type": "integer",
"example": 1
}
}]
}
}
}
}
One way to get around this error is to change type from integer to string.
"parameters": [{
"in": "body",
"name": "role",
"required": true,
"schema": {
"type": "string",
"example": "1"
}
}]
I'm trying to convert a JSON payload to Avro to publish to a Kafka topic. However, when I do the Dataweave transformation I'm getting a "No Type" error. I'm not sure what's causing the error. I originally thought this might be due to the transformation not knowing what the MIME type on the inbound payload. So, I've made sure that it's set to application/json but that didn't make any difference.
Avro Schema
{
"compatibility" : "forward",
"name": "ContentManagerCoupons",
"type": "record",
"namespace": "com.rentpath",
"fields": [
{
"name": "clientID",
"type": "string"
},
{
"name": "outputHistoryId",
"type": "string"
},
{
"name": "categoryCoupons",
"type": {
"type": "array",
"items": {
"name": "categoryCoupons_record",
"type": "record",
"fields": [
{
"name": "applyBy",
"type": [
"string",
"int",
"null"
]
},
{
"name": "applyPeriod",
"type": [
"string",
"null"
]
},
{
"name": "cashValue",
"type": [
"int",
"null"
]
},
{
"name": "couponCategory",
"type": "string"
},
{
"name": "cashOffDesc",
"type": [
"string",
"null"
]
},
{
"name": "endDate",
"type": [
"string",
"null"
]
},
{
"name": "feeType",
"type": [
"string",
"null"
]
},
{
"name": "freeWeeks",
"type": [
"string",
"null"
]
},
{
"name": "generatedText",
"type": "string"
},
{
"name": "leaseby",
"type": [
"string",
"null"
]
},
{
"name": "leaseTerm",
"type": [
"int",
"null"
]
},
{
"name": "offerText",
"type": [
"string",
"null"
]
},
{
"name": "startDate",
"type": "string"
},
{
"name": "unitType",
"type": [
"string",
"null"
]
}
]
}
}
}
]
}
JSON Message
{
"outputHistoryId": "55324456",
"clientID": "112345",
"categoryCoupons": [
{
"unitType": null,
"startDate": "07/21/2020",
"offerText": "This would be the special offer message.",
"leaseTerm": null,
"leaseby": null,
"generatedText": "This would be the special offer message..",
"freeWeeks": null,
"feeType": null,
"endDate": "10/01/2020",
"couponCategory": "Special Offer",
"cashValue": null,
"cashOffDesc": null,
"applyPeriod": null,
"applyBy": null
}
]
}
Datawave
%dw 2.2
output application/avro schemaUrl="http://schema-registry.domain.com:8081/subjects/Coupon-value/versions/1"
---
payload
Error Message
"org.apache.avro.SchemaParseException - No type: {"subject":"ContentManager.Coupon-value","version":1,"id":342,"schema":"{"type":"record","name":"ContentManagerCoupons","namespace":"com.rentpath","fields":[{"name":"clientID","type":"string"},{"name":"outputHistoryId","type":"string"},{"name":"categoryCoupons","type":{"type":"array","items":{"type":"record","name":"categoryCoupons_record","fields":[{"name":"applyBy","type":["string","int","null"]},{"name":"applyPeriod","type":["string","null"]},{"name":"cashValue","type":["int","null"]},{"name":"couponCategory","type":"string"},{"name":"cashOffDesc","type":["string","null"]},{"name":"endDate","type":["string","null"]},{"name":"feeType","type":["string","null"]},{"name":"freeWeeks","type":["string","null"]},{"name":"generatedText","type":"string"},{"name":"leaseby","type":["string","null"]},{"name":"leaseTerm","type":["int","null"]},{"name":"offerText","type":["string","null"]},{"name":"startDate","type":"string"},{"name":"unitType","type":["string","null"]}]}}}],"compatibility":"forward"}"}
org.apache.avro.SchemaParseException: No type: {"subject":"ContentManager.Coupon-value","version":1,"id":342,"schema":"{"type":"record","name":"ContentManagerCoupons","namespace":"com.rentpath","fields":[{"name":"clientID","type":"string"},{"name":"outputHistoryId","type":"string"},{"name":"categoryCoupons","type":{"type":"array","items":{"type":"record","name":"categoryCoupons_record","fields":[{"name":"applyBy","type":["string","int","null"]},{"name":"applyPeriod","type":["string","null"]},{"name":"cashValue","type":["int","null"]},{"name":"couponCategory","type":"string"},{"name":"cashOffDesc","type":["string","null"]},{"name":"endDate","type":["string","null"]},{"name":"feeType","type":["string","null"]},{"name":"freeWeeks","type":["string","null"]},{"name":"generatedText","type":"string"},{"name":"leaseby","type":["string","null"]},{"name":"leaseTerm","type":["int","null"]},{"name":"offerText","type":["string","null"]},{"name":"startDate","type":"string"},{"name":"unitType","type":["string","null"]}]}}}],"compatibility":"forward"}"}
at org.apache.avro.Schema.getRequiredText(Schema.java:1753)
at org.apache.avro.Schema.parse(Schema.java:1604)
at org.apache.avro.Schema$Parser.parse(Schema.java:1394)
at org.apache.avro.Schema$Parser.parse(Schema.java:1365)
at org.mule.weave.v2.module.avro.AvroWriter.doWriteValue(AvroWriter.scala:195)
at org.mule.weave.v2.module.writer.Writer.writeValue(Writer.scala:41)
at org.mule.weave.v2.module.writer.Writer.writeValue$(Writer.scala:39)
at org.mule.weave.v2.module.avro.AvroWriter.writeValue(AvroWriter.scala:44)
at org.mule.weave.v2.module.writer.DeferredWriter.doWriteValue(DeferredWriter.scala:73)
at org.mule.weave.v2.module.writer.Writer.writeValue(Writer.scala:41)
at org.mule.weave.v2.module.writer.Writer.writeValue$(Writer.scala:39)
at org.mule.weave.v2.module.writer.DeferredWriter.writeValue(DeferredWriter.scala:16)
at org.mule.weave.v2.module.writer.WriterHelper$.writeValue(Writer.scala:120)
at org.mule.weave.v2.module.writer.WriterHelper$.writeAndGetResult(Writer.scala:98)
at org.mule.weave.v2.interpreted.InterpretedMappingExecutableWeave.write(InterpreterMappingCompilerPhase.scala:236)
at org.mule.weave.v2.el.WeaveExpressionLanguageSession.evaluateWithTimeout(WeaveExpressionLanguageSession.scala:243)
at org.mule.weave.v2.el.WeaveExpressionLanguageSession.evaluate(WeaveExpressionLanguageSession.scala:108)
at org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor$1.evaluate(DataWeaveExpressionLanguageAdaptor.java:308)
at org.mule.runtime.core.internal.el.DefaultExpressionManagerSession.evaluate(DefaultExpressionManagerSession.java:105)
at com.mulesoft.mule.runtime.core.internal.processor.SetPayloadTransformationTarget.process(SetPayloadTransformationTarget.java:32)
at com.mulesoft.mule.runtime.core.internal.processor.TransformMessageProcessor.lambda$0(TransformMessageProcessor.java:92)
at java.util.Optional.ifPresent(Optional.java:159)
at com.mulesoft.mule.runtime.core.internal.processor.TransformMessageProcessor.process(TransformMessageProcessor.java:92)
at org.mule.runtime.core.api.util.func.CheckedFunction.apply(CheckedFunction.java:25)
at org.mule.runtime.core.api.rx.Exceptions.lambda$checkedFunction$2(Exceptions.java:84)
at org.mule.runtime.core.internal.util.rx.Operators.lambda$nullSafeMap$0(Operators.java:47)
at reactor.core.publisher.FluxHandleFuseable$HandleFuseableSubscriber.onNext(FluxHandleFuseable.java:165)
at org.mule.runtime.core.privileged.processor.chain.AbstractMessageProcessorChain$2.onNext(AbstractMessageProcessorChain.java:425)
at org.mule.runtime.core.privileged.processor.chain.AbstractMessageProcessorChain$2.onNext(AbstractMessageProcessorChain.java:420)
at reactor.core.publisher.FluxHide$SuppressFuseableSubscriber.onNext(FluxHide.java:127)
at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:204)
at reactor.core.publisher.FluxOnAssembly$OnAssemblySubscriber.onNext(FluxOnAssembly.java:345)
at reactor.core.publisher.FluxSubscribeOnValue$ScheduledScalar.run(FluxSubscribeOnValue.java:178)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:50)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:27)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.mule.service.scheduler.internal.AbstractRunnableFutureDecorator.doRun(AbstractRunnableFutureDecorator.java:111)
at org.mule.service.scheduler.internal.RunnableFutureDecorator.run(RunnableFutureDecorator.java:54)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748), while writing Avro at payload.
It seems to work for me. Maybe there is a problem trying to access the schema. Because I don't have access to that URL I replaced it with a local file:
output application/avro schemaUrl="classpath://schema.json"
Apparently the answer is pretty simple. I just needed to add schema to the end of my URL. This removes the extraneous items like version and id that come down without it.
New Dataweave
%dw 2.2
output application/avro schemaUrl="http://schema-registry.domain.com:8081/subjects/Coupon-value/versions/1/schema"
---
payload
I have the below json file.
1) I renamed it as mytest.environment.json and I am trying to import the same in postman client app. I am getting an error - format not recognized. (all values are null intentionally)
2) I am not able to connect https://api.getpostman.com/environments to create an environment.
3) If this can not be done, how can I tie an environment name to a request inside test script? For e.g, I have a collection, which needs to run against multiple environments . I could not find any API which logs environment.name. When I do console.log(environment), it just prints that its an object.
{
"environment": {
"name": "Test Environment",
"US_DC": [{
"enabled": true,
"key": "oauth_server",
"value": "",
"type": "text"
},
{
"enabled": true,
"key": "oauth_secret",
"value": "",
"type": "text"
},
{
"enabled": true,
"key": "grant_type",
"value": "",
"type": "text"
},
{
"enabled": true,
"key": "api_gateway",
"value": "",
"type": "text"
}
],
"EU_DC": [{
"enabled": true,
"key": "oauth_server",
"value": "",
"type": "text"
},
{
"enabled": true,
"key": "oauth_secret",
"value": "",
"type": "text"
},
{
"enabled": true,
"key": "grant_type",
"value": "",
"type": "text"
},
{
"enabled": true,
"key": "api_gateway",
"value": "",
"type": "text"
}
]
}
}
Swagger file is working as expected with warnig.
{
'swagger': "2.0",
"info": {
"version": "3.0",
"title": "Sample Service",
},
"schemes": [ "http" ],
"host": "sampleservice.azurewebsites.net",
"paths": {
"/": {
"post": {
"summary": "Sample service",
"description": "sample service",
"parameters": [
{
"name": "Input",
"in": "body",
"description": "valid input",
"schema": {
"type": "object",
"properties": {
"MainAttr-1": {
"required": [ "Attr-1" ],
"type": "object",
"properties": {
"Attr-1": {
"description": "Attr-1",
"required": true,
"type": "string"
},
"Attr-2": {
"description": "Attr-2",
"required": false,
"type": "string"
},
"Attr-3": {
"description": "Attr-3",
"required": false,
"type": "boolean"
},
"Attr-4": {
"description": "Attr-4",
"required": false,
"type": "boolean"
},
"Attr-5": {
"description": "Attr-5",
"required": false,
"type": "string"
}
}
},
"MainAttr-2": {
"type": "array",
"items": {
"type": "object",
"required": [ "Attr-1", "Attr-3", "Attr-5", "Attr-8" ],
"properties": {
"Attr-1": {
"description": "Attr-1",
"required": true,
"type": "string"
},
"Attr-2": {
"description": "Attr-2",
"required": false,
"type": "string"
},
"Attr-3": {
"description": "Attr-3",
"required": true,
"type": "boolean"
},
"Attr-4": {
"description": "Attr-4",
"required": false,
"type": "boolean"
},
"Attr-5": {
"description": "Attr-5",
"required": true,
"type": "string"
},
"Attr-6": {
"description": "Attr-6",
"required": false,
"type": "string"
},
"Attr-7": {
"description": "Attr-7",
"required": false,
"type": "string"
},
"Attr-8": {
"description": "Attr-8",
"required": true,
"type": "string"
}
}
}
}
}
}
}
],
"responses": {
"200": {
"description": "success"
}
}
}
}
}
}
Issue-1) Warning should be removed
Issue-2) Attr-3 in "MainAttr-2" is boolean type attribute and it is required. But when i am selecting 'false' from the dropdown, it is treating as invalid input. It treats only 'true' as valid input. (Any Boolean attribute which is required behaving like this)
Due this warning i am unable to deliver the code.
Thanks in advance.
In the beginning, change 'swagger' to "swagger". JSON requires double quotes around strings.
Remove extra comma at the end of:
"title": "Sample Service",
Remove the required attribute from ALL property definitions (Attr-1 to Attr-8) and use the required list at the object level instead (under MainAttr-1 and MainAttr-2).
"MainAttr-1": {
"required": [ "Attr-1" ], <-- list the required properties in this array
"type": "object",
"properties": {
"Attr-1": {
"description": "Attr-1",
"required": true, <-- remove this
"type": "string"
},
Other than that your spec is fine.
I am newer in Swagger tool. I try to test my Restfull application with swagger editor. I used basic authentication to access the web service.
In the Swagger-UI, the preview looks correct, i.e. Content-Type: application/json and the json is in the body.But When I send GET request from the Swagger editor to server, I got an error.
ERROR Server not found or an error occurred
My Swagger
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore (Simple)",
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://helloreverb.com/terms/",
"contact": {
"name": "Swagger API team",
"email": "abc#gmail.com",
"url": "http://avfg.com"
},
"license": {
"name": "MIT",
"url": "http://opensource.org/licenses/MIT"
}
},
"host": "127.0.0.1:8xxx",
"basePath": "/v1",
"schemes": [
"http"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/facedetect/{username}/{albumname}/{imagename}": {
"get": {
"description": "Returns all pets from the system that the user has access to",
"operationId": "findPets",
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"name": "username",
"in": "path",
"description": "tags to filter by",
"required": true,
"type": "string"
},
{
"name": "albumname",
"in": "path",
"description": "maximum number of results to return",
"required": true,
"type": "string"
},
{
"name": "imagename",
"in": "path",
"description": "maximum number of results to return",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "pet response",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/pet"
}
}
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/errorModel"
}
}
}
}
}
},
"definitions": {
"pet": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"errorModel": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
Please help me.
Thanks in advance.
Make sure that you have your server running.
If you have swagger installed, you can do
swagger project start
I got the solution.
Its CORS issue. My browser was blocking cors requests. I have installed a Chrome extension that adds Access-Control-Allow-Origin to outgoing requests.
I had simillar problem
this is CORS, but :
in nodeJS i have set:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, UPDATE, DELETE, OPTIONS')
res.header("Content-Type", "application/json");
next();
});
but it helps only when request was without authorization (api-key). To make it works I have to change and use:
const cors = require('cors');
app.use(cors());
hope it is helpfull