I'm using swagger akka-http wrapper, Currently for my get request swagger is adding additional body parameter in swagger spec of type string
#Path("/{id}/status")
#ApiOperation(httpMethod = "GET", response = classOf[JobStatus], value = "Returns Job status")
#ApiImplicitParams(Array(
new ApiImplicitParam(name = "id", required = true, dataType = "integer", paramType = "path", value = "Job id for which status be fetched")))
#ApiResponses(Array(
new ApiResponse(code = 200, message = "OK", response = classOf[JobStatus]),
new ApiResponse(code = 404, message = "Job not found")))
def getStatus(id: String): Route =
get {
....
I'm wondering this is because of getStatus method taking parameter "id", Do any one have any suggestion
The generated docs are based on the both the function parameters and the implicit parameters (ie the union of the 2 parameter sets).
I would suggest that you remove the ApiImplicitParam annotation and add an ApiModelProperty annotation to the id field in the function parameter list if you need to override its declared type of String.
Example use of ApiModelProperty annotation:
https://github.com/pjfanning/swagger-akka-http-sample/blob/master/src/main/scala/com/example/akka/addoption/AddOptionActor.scala
Related
To edit the entity training, I use the TrainingFormType like this:
public function editTraining(Training $training) {
$form = $this->createForm(TrainingFormType::class, $training);
$form->submit($request->request->all());
The form has a field title:
->add('title', TextType::class, [
'constraints' => [
new NotBlank(),
new Length(null, null, 50)
]])
If I pass in the request an empty value into the field title "title" : "", I get an error 500:
"Expected argument of type "string", "null" given at property path "title".". I expected here an error from the form, not the InvalidArgumentException
When using the same form for creating of a new training, everything works fine, a validation error is generated, as expected:
"title": {
"errors": [
"This value should not be blank."
]
},
It's how I use the form when creating a new training:
$training = new Training();
$form = $this->createForm(TrainingFormType::class, $training);
$form->submit($request->request->all());
How to solve this problem? I thought, I can resolve it using an so called IgnoreNonSubmittedFieldListener, but on this way it also doesn't work. I get the same error.
One possible solution is to set a sensible default in the Training entity for that field:
// in Training.php
// ORM mapping omitted:
private string _title = '';
Then when a new entity is created it will have that value.
I need to specify that my endpoint has a mandatory field, an optional field and open to any number of fields(which can be sent without validation).
For e.g. for an endpoint /user
user_id: str, mandatory
timestamp_utc: timestamp, optional
..*accept_any_extra_fields**..
So if someone sends the following json to my endpoint, the endpoint should accept it
{ "user_id": 1,
"name": "Sam",
"location": "USA"
}
but it fails if the following json is sent as it does not contain user_id.
{ "name": "Sam",
"location": "USA"
}
it should fail.
I'm new to OpenAPI/Swagger. I know I can send the extra data. But how do I describe this as documentation on OpenAPI so a person(or a program) knows that they can send any field (e.g. name, location) along with user_id
Do you use Java-Spring? I use Swagger in Annotation approach in my Spring controllers, and in java code, you can specify params that you need in this way:
#ApiOperation(value = "Get user", notes = "Get a user by the given filters")
#GetMapping("v1/users")
public UserDTO getUsers(#ApiParam(value = "User id", required = true)
#RequestParam(value = "user_id", required = true) String userId,
#ApiParam(value = "Date", required = false)
#RequestParam(value = "timestamp_utc", required = false)
#DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime utc,
#ApiParam(value = "Some other", required = false)
#RequestParam(value = "some_other", required = false) String someOther){
return service.getUser(userId, utc, someOther);
}
The annotation #ApiOperation is to describre your endpint.
The annotation #ApiParam is to describe the param characteristics, and the attribute required is to inform that is.
Don't forget to add the swagger dependencies, here is on maven.
You also have the possibility to generate your API documentation in YAML. An example is here . Please check the endpoint for user/login.
I hope my answer will help you.
The additionalProperties keyword allows a schema to have extra properties besides those listed in the properties section.
MyModel:
type: object
required:
- user_id
properties:
user_id:
type: string
timestamp_utc:
type: string
additionalProperties: true # OpenAPI 3.0
# or
# additionalProperties: {} # OpenAPI 2.0
Actually OpenAPI schemas are open to extension by default, in the absence of the additionalProperties keyword. However, some tools consider the absence of additionalProperties as "additional properties NOT allowed", so it's best to add additionalProperties: true / additionalProperties: {} explicitly just in case.
If the extra properties are limited to a specific data type, e.g. string, use
additionalProperties:
type: string
I have written below Resource Class:
#clinic_api_ns.route("/patient/profile")
class PatientProfile(Resource):
def get(self):
patient = request.args.get(patient)
relative = request.args.get(relative)
relationType = request.args.get(relationType)
contactNumber = request.args.get(contactNumber)
townCity = request.args.get(townCity)
return controller.get_patient_profile(patient, relative, relationType, contactNumber, townCity)
so that to get patient profile , I can use passed parameters through URL such as http://ip.address/api/clinic/patient/profile?patient=<patientName>&relative=<relativeName>&relationType=<relation>
but this throws error in swagger documentation and even if I tried adding #_api_ns.expect(patient_profile, validate=True)
where patient_profile is
class PatientProfile(object):
profile = clinic_api_ns.model("profile", {
"patient": fields.String(required=True, description="name of the patient."),
"relative": fields.String(required=True, description="name of parent or husband."),
"relation": fields.String(required=True, description="type of relation with patient."),
"contactnumber": fields.String(required=True, description="contact number of the patient."),
"townCity": fields.String(required=True, description="town or city the patient belongs to."),
})
You can use parser to solve this.
parser = api.parser()
parser.add_argument('param', type=int, help='Some param', location='path') # "location='path'" means look only in the querystring
#api.expect(parser)
Below is the REST api which i want to document using Swagger UI
#ApiOperation(
nickname = "alertForBundleId",
value = "alertForBundleId",
notes = "",
httpMethod = "GET")
def alertForBundleId(
#ApiParam(name = "mfr", value = "Manufacturer", required = true) #PathParam("mfr") mfr: String,
#ApiParam(name = "col", value = "Columns", required = true) #QueryParam("col") cols: List[String])){...}
Here cols parameter is accepting List of string.When i pass two elements for the list separating by new line, then its generates url like ?col=sysid%2Calert_idwhile it should be like ?col=sysid&col=alert_idit works well for single element listAny help will be greatly appreciated.
Why isn't the value of default-only variable myVar added to outbound URL as part of a query string:
routes.MapRoute("MyRoute", "{controller}/{action}",
new { myVar = 1 });
Index.cshtml:
#Html.ActionLink("click me", "CustomVariable", "Home",
new { myVar = 1, newMyVar = 1000 }, null);
From the above routing system generates:
click me
Thank you
In your routing rule you are stating that if the default value is not provided, then use this value.
So there is really no need to provide the value when it is the same as the default value, because it will simply use the default value.