Add and remove arbitrary input fields from Swagger UI in Spring Boot - spring

I have a Spring Boot application with springfox-swagger-ui. I need to:
Make Swagger UI display input field for a parameter that does not appear among the request handler params in my controller. For example, the request contains a hashCode parameter, which I verify with a HandlerInterceptorAdapter and then I don't need anymore, thus I don't list it among the request handler params. But I want to have it in Swagger UI.
Remove an input field from Swagger UI that corresponds to a parameter that I have in my request handler in my controller. For example, the request contains a userId, which I use to construct an User object with a HandlerMethodArgumentResolver and then inject it into the request handler by adding a parameter of type User. Thus I don't want Swagger UI to create User input field, but instead a userId input field.
To summarize, I need to show an input field in Swagger UI that does not correspond to any request handler parameter, and I need to hide an input field in Swagger UI that corresponds to a request handler parameter. How to do this?

Related

Spring Rest docs ignore request body fields in documentation

I have the followin situation:
My Endpoint consumes an Object A that results in the backend depending on the requesting user to Object B or C. So the problem is, that Object A has all the fields of Object B and C. As a result I want to ignore request fields for a specific user group because they are always null and are irgnored in the backend.
Is there a way to set a .ignore option for request fields so I can provide the example JSON request body when using the http-request.snippet?

Can API-Gateway check the input parameters and reject (without passing it to Lambda) an HTTP request?

I would like to use API-Gateway (plus Lambda) to realize a PetStore Restful API. One of the API entry point is and the body of the POST request needs to contain 'name'. See the PetStore definition snippet below:
/pets/
POST
definitions:
NewPet:
required:
- name
properties:
name:
type: string
tag:
type: string
Can I make API-Gateway smart enough to reject requests that does not contain the required field (in this case, 'name'), without calling the Lambda (yes, I understand I am able to check the input fields inside the Lambda function, but I wonder if I can avoid it)? -- this would save a lot of time and complexity to deal with the corner case.
This can be achieved through using request validation set in the "method request" settings.
If you want to validate the request body rather than query string you need to create a model;
APIs > {ApiName} > Models
In the sidebar.
Create your model using JSON Schema
In the API Gateway, choose to validate the request body then select your created model in the dropdown.
API Gateway config page

Struts - binding form fields to bean

Lets say I have couple of filters in my deployment descriptor and each request to the app has to pass through those. There is form with a formbean attached to it. Once I fill the form and click submit, when does the html parameters in the form are binded to the form bean? Before the request is passed through the filters or after passing thorugh the filters?
The reason I am asking is.. I have a form with 20 fields. Once I click submit, 17 values are being binded to the bean properly but 3 values are missing by the team request reaches validate method. I am absolutely clueless has to where the values are missing. I tried to print an enumeration of all the request parameters in validate method and those three fields are there in request object. AFAIK, There is no issue with those three elements name because the app works perfecly in my local IDE but brakes up in test env which makes it all the more difficult to pin-point the issue :(.

Spring Passing request parameter

I want to pass parameter (a String value) to spring controller from a jsp
What is the best or recommended way to do it.
-I don't want to send parameters in URL - #RequestParam may not be suitable
-Should I hook the parameter to a model object and use #ModelAttribute. What if I want just a string value to be passed.. should I create a object with just a string attribute for this purpose?
-Use HttpSevletRequest
Use #RequestParam - it will work with both GET and POST requests. So if you don't want to send them in the URL, use the POST method to submit your form.

Validate request headers with Spring validation framework

Is it possible to use the Spring validation framework with Spring MVC to validate the presence and value of an HTTP request header?
To check the presence of a request header, you don't need the validation framework. Request header parameters are mandatory by default, and if a mandatory header is missing in a request, Spring MVC automatically responds with 400 Bad Request.
So the following code automatically checks the presence of the header "Header-Name"...
#PostMapping("/action")
public ResponseEntity<String> doAction(#RequestHeader("Header-Name") String headerValue) {
// ...
}
... and if the header shall be optional, the annotation would need to be replaced by:
#RequestHeader(name = "Header-Name", required = false)
To check the value of a request header, the Spring validation framework can be used. To do this, you need to
Add #Validated to the controller class. This is a workaround needed until this feature is implemented.
Add the JSR-303 annotation to the request header parameter, e.g.
#RequestHeader("Header-Name") #Pattern(regexp = "[A-Za-z]*") String headerValue
Note however that this will result in a 500 in case of an invalid header value. Check this question for how to also get the correct status code (i.e. 400) for this case.
I don't see how this would be possible, since the validation framework only operates on your domain objects, not on the HTTP request itself. Specifically, the Validator interface doesn't specify any methods that take the HttpServletRequest object, which is what you'd need to have access to in order to grab the headers and test them.
Using the validation framework feels like the wrong solution to whatever problem you're trying to solve, especially since it's hard to know how there'd be a unique HTTP request header for a given form submission. Are you looking to test for an HTTP header that should always be present in requests to your app? Then you might want to consider implementing a HandlerInterceptor, which will intercept and process all requests to pages that you've mapped in any HanderMappings. Are you looking to test for an HTTP header that should always be present in any page view of your app? Then you'd want to implement a Filter, which operates outside of the context of Spring MVC.

Resources