I am using Swagger for my api doc. Some Api use dynamic post object, so I just use:
parameters:
- name: region
in: body
schema:
type: object
In swagger UI, I can edit the content using "Edit Json" button, but after I click "Save", nothing append to request, it still send empty request when I click the "Send Request" button. So, what am I wrong?
And I test this on http://editor.swagger.io/, change a post parameter to "object" type , then still can not edit json data.
You can use additionalProperties to define an object with dynamic properties. For example:
parameters:
- name: region
in: body
schema:
type: object
additionalProperties:
type: object
That says the request body is a JSON object with no fixed properties. Clients can specify properties of any type (string, boolean, object, etc.) at run time. See OpenAPI specification v2.0 for more on additionalProperties.
Related
I have multipart/form-data request as I want to upload a file.Now I want to send data with file and I am using form-data.One of the field is array of object, So now how can I pass array of object in formData in swagger UI with cakephp.
Vishal, you can use serialize() for this and pass the serialize variable on the form element, its behave like string and after it you can use unserialize().
$string_var = serialize(Array);
$Array = unserialize(string_var);
We are trying to achieve a scenario on the Swagger UI in the Body section. In the Requests section, can we have an Example Value JSON hiding one or more fields but the Model would still show those fields?
We are basically trying to reduce the number of fields in the request body but have all the fields visible in the Model.
For example, we would like to hide the name in the example here:
but still display the name in the Model here:
To hide fields from auto-generated model examples in Swagger UI, you'll need to add a custom example for that model that includes only the fields you need.
definitions:
Pet:
type: object
properties:
...
# Override model example that will be displayed in Swagger UI
example:
id: foo
status: available
I'm integrating dropwizard-swagger into a large existing project.
I've got the Swagger UI endpoint up and running now, but I'm noticing that it seems adamant that every method must have a body parameter.
Specifically, the first parameter in the method definition that doesn't have an #ApiParam annotation gets interpreted as a request body. There doesn't appear to be a way to specify a body parameter, nor does there seem to be a way to exclude parameters from being labelled as such automatically by Swagger UI. This means that the "Try it Out" functionality doesn't work for a large portion of the endpoints, as bodies are disallowed by the spec but Swagger UI keeps insisting they are present.
For example, consider the below method in the UserResource file:
#GET
#Path("v1/users/{userId}/subscriptions")
#ApiOperation(value = "Get user subscriptions", notes = "Returns information about the users current and past subscriptions.")
#UserAccessRequired
#RolesAllowed({//a list of appropriate roles})
#Produces(CompanyMediaType.APPLICATION_API_V1_JSON)
public SubscriptionsDTOV1 getSubscriptionsForUser(#Auth DashboardUser dashboardUser, #JooqInject DSLContext database,
#Context ResourceContext resourceContext,
#Context ContainerRequestContext crc,
#ApiParam(value = "ID of user", type = "Integer") #NotNull #UnwrapValidatedValue #PathParam("userId") IntParam userId) {
Swagger is interpreting the first parameter, #Auth DashboardUser dashboardUser, as being the request body, and generating the below view in Swagger UI:
Swagger UI with a body parameter
Since this is a GET, it's not permitted to have a body, and attempting to delete the contents of the body in the Swagger UI while testing it out doesn't work, as the field autofills with {}.
How do I indicate to Swagger that there is no body parameter here and get this to work? Just putting #ApiParam in front of the other method parameters doesn't work, as that annotation is ignored if there isn't also a #QueryParam/#PathParam/etc annotation present.
Turns out adding #ApiParam(hidden=true) seems to have done the trick. I tried this before and didn't get results, must have just had a typo somewhere.
I have an apache camel route that is making an HTTP POST request i.e.
from(...).setHeader(Exchange.HTTP_METHOD, constant(POST)).to("http4://myUrl?...");
The request URL includes query params (unusual for POST i know, but I have no choice here) that I need to populate from details of the exchange which are stored on the body.
i.e. The body is a POJO like so:
public class Params {
String param1;
int param2;
....
//etc. etc. including getters and setters
}
Where each field is either a primitive or string, and refers directly to an equivalent query parameter:
http4://myUrl?param1=...¶m2=...&...
Is there a way I can avoid having to manually define every parameter on the URL and instead automagically map the exchange body to query params on the request being made?
The reason I need to do this is that some of the query params are optional, and should be populated based on the contents of the exchange body.
You can set the header Exchange.HTTP_QUERY with the query parameters separated.
And its not possible to automatic map from a message bodies its fields to URI parameters (no magic included). You would need to build some code that computes the URI query with & separating the values, and setting that as the HTTP_QUERY header.
I need to make oAuth multipart request message for the API which requires some query string parameters as well as Multipart Post Part.
But there is no PrepareAuthorizedRequest Method which accepts both IDictionary type query string parameters and MultipartPostPart fields.
What should I do?
Simply create a MessageReceivingEndpoint whose URI already includes those query string parameters.