Swagger Codegen Java RequestBody Name - spring

I am using the swagger-codegen Spring generator to generate the API classes using a YAML API definition. I am describing post request with multiple parameters in the body like this:
paths:
/api/test:
post:
...
requestBody:
content:
application/json:
schema:
properties:
firstName:
type: string
lastName:
type: string
This generates a method in the ApiDelegate with the signature ResponseEntity test(Body body). However, when describing multiple post methods like this, the subsequent generated signatures have parameters of type Body1, Body2, ... . With Swagger 2.0, it was possible to name the RequestBodies. Can I do something similar with V3?
Thanks for your help!
Kind regards

Related

In a serverless.yml file, how does one customize the default http response template?

Looking at the documentation here https://serverless.com/framework/docs/providers/aws/events/apigateway/#custom-response-templates, there doesn't seem to be much detail at all about setting up these templates.
I'm currently looking to remove the default application/json content-type, which is generated during creation of the handler's integration response (pictured below), and replace it with text/html. Is there defined syntax for how to do this hidden somewhere? Or is this level of customization not possible within the current scope of the framework?
Here is my endpoint as defined in serverless.yml
events:
- http:
method: any
path: /
integration: lambda
request:
region: ${env:AwsRegion}
response:
headers:
Content-Type: "'text/html'"
template: $input.path('body')
- http:
method: any
path: /{proxy+}
Which produces the following configuration on AWS Api Gateway:
I did try modifying the specification like this, as a guess, but it threw a syntax error:
template:
text\html: $input.path('body')
Make It Work
It looks like this isn't really supported by the framework, but it can be hacked together by (ab)using statusCodes in your serverless template.
Moving the response template to under a status code, and providing a pattern for that status code, accomplishes what I think you are after. The syntax:
- http:
method: any
path: /
integration: lambda
request:
region: us-east-1
response:
headers:
Content-Type: "'text/html'"
statusCodes:
200:
pattern: ""
template:
text/html: $input.path('body')
Note: Both the pattern and the template must be present.
Is It Really That Bad?
That's up to you, ultimately. I'm calling it a hack because:
It would be much nicer to be able to supply this at the response.template level, rather than at response.statusCodes.200.template.
Specifying one or more statusCodes removes the set of default lambda error regexes that you get when you don't specify any.
It feels like working around the fact that response.template will only accept a string, whereas it could (should?) accept a string or an object (like it does under statusCodes).
Fix It?
The offending code, from /lib/plugins/aws/package/compile/events/apiGateway/lib/method/integration.js:
if (http.response.template) {
_.merge(integrationResponse.ResponseTemplates, {
'application/json': http.response.template,
});
}
if (config.template) {
const template =
typeof config.template === 'string'
? { 'application/json': config.template }
: config.template;
_.merge(integrationResponse.ResponseTemplates, template);
}
I think for this to work under response.template, the code in the first if() would need to behave more like the code in the second if().

#AO\JsonContent in parameter of get request

In swagger's documentation using OpenApi specification, you can wrap schema in parameter to content with application/json:
parameters:
- in: query
name: filter
# Wrap 'schema' into 'content.<media-type>'
content:
application/json: # <---- media type indicates how to serialize / deserialize the parameter content
schema:
type: object
properties:
type:
type: string
color:
type: string
to send objects like this filter={"type":"t-shirt","color":"blue"}.
How to do it in swagger-php?
The swagger-editor and swagger-ui have added support for http://swagger.io/docs/specification/describing-parameters
The swagger-php library has added support too, it will be part of the 3.0.4 release.

Swagger does not consider, that body is not required

I define endpoints with Swagger file, and for one of the endpoints the body (a JSON) is not required.
I describe it in this way in the yaml file:
- in: "body"
name: "body"
description: "some description here..."
required: false
schema:
type: string
After that I generate code from the Swagger file and for the body I get the following description in the method for this endpoint in the generated class:
#Valid #RequestBody String body
the generated file is an interface, and when I implement the method, and call the endpoint without setting a body, I get:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing:
Can somebody tell me what I am doing wrong? When I put
#RequestBody(required=false) String body
, then everything works correct, but I would like this (required=false) to be generated automatically from the code generator...

Passing a path parameter to Google's Endpoint for Cloud Function

I am following Google's tutorial on setting up Google Cloud endpoint (not AWS API Gateway) in front of my Cloud Function. I am triggering my Cloud Function to trigger an AWS lambda function, AND I am trying to pass a path parameter from my Endpoint as defined by OpenAPI spec.
Path parameters are variable parts of a URL path. They are typically used to point to a specific resource within a collection, such as a user identified by ID. A URL can have several path parameters, each denoted with curly braces { }.
paths: /users/{id}:
get:
parameters:
- in: path
name: id # Note the name is the same as in the path
required: true
schema:
type: integer
GET /users/{id}
My openapi.yaml
swagger: '2.0'
info:
title: Cloud Endpoints + GCF
description: Sample API on Cloud Endpoints with a Google Cloud Functions backend
version: 1.0.0
host: HOST
x-google-endpoints:
- name: "HOST"
allowCors: "true
schemes:
- https
produces:
- application/json
paths:
/function1/{pathParameters}:
get:
operationId: function1
parameters:
- in: path
name: pathParameters
required: true
type: string
x-google-backend:
address: https://REGION-FUNCTIONS_PROJECT_ID.cloudfunctions.net/function1
responses:
'200':
description: A successful response
schema:
type: string
The error I get when I use Endpoint URL https://REGION-FUNCTIONS_PROJECT_ID.cloudfunctions.net/function1/conversations is a TypeError from my AWS lambda function
StatusCode:200, FunctionError: "Unhandled", ExecutedVersion: "$LATEST". Payload: "errorType":"TypeError", errorMessage:"Cannot read property 'startsWith' of undefined..."
It is saying that on line
var path = event.pathParameters;
...
...
if (path.startsWith('conversations/'){...};
my path var is undefined.
I initially thought my Google Function was not correctly passing pathParameters but when I tested my Google function using triggering event {"pathParameters":"conversations"}, my Lambda returns the payload successfully.
My Google Cloud Function:
let AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'region'
})
let lambda = new AWS.Lambda();
exports.helloWorld = async(req,res) => {
let params = {
FunctionName:'lambdafunction',
InvocationType: "RequestRespone",
Payload: JSON.stringify(req.body)
};
res.status(200).send(await lambda.invoke(params, function(err,data){
if(err){throw err}
else{
return data.Payload
}
}).promise());
}
EDIT 1:
Seeing this Google Group post, I tried adding to my openapi.yaml file path_translation: APPEND_PATH_TO_ADDRESS, yet still I'm having no luck.
...
paths:
/{pathParameters}:
get:
...
x-google-backend:
address: https://tomy.cloudfunctions.net/function-Name
path_translation: APPEND_PATH_TO_ADDRESS
#Arunmainthan Kamalanathan mentioned in the comments that testing in AWS and Google Cloud directly with trigger event {"pathParameters":"conversations"} is not equivalent to passing req.body from my Google function to AWS lambda. I think this is where my error is occurring -- I'm not correctly passing my path parameter in the payload.
EDIT 2:
There is this Stackoverflow post concerning passing route parameters to Cloud Functions using req.path. When I console.log(req.path) I get / and console.log(req.params) I get {'0': '' }, so for some reason my path parameter is not getting passed correctly from Cloud Endpoint URL to my Google function.
I was running into the same issue when specifying multiple paths/routes within my openapi.yaml. It all depends on where you place the x-google-backend (top-level versus operation-level). This has implications on the behaviour of the path_translation. You could also overwrite path_translation yourself, as the documentation clearly describes:
path_translation: [ APPEND_PATH_TO_ADDRESS | CONSTANT_ADDRESS ]
Optional. Sets the path translation strategy used by ESP when making
target backend requests.
Note: When x-google-backend is used at the top level of the OpenAPI
specification, path_translation defaults to APPEND_PATH_TO_ADDRESS,
and when x-google-backend is used at the operation level of the
OpenAPI specification, path_translation defaults to CONSTANT_ADDRESS.
For more details on path translation, please see the Understanding
path translation section.
This means that if you want the path to be appended as a path parameter instead of a query parameter in your backend, you should adhere to the following scenario's:
Scenario 1: Do you have one cloud function in the x-google-backend.address that handles all of your paths in the openapi specification? Put x-google-backend at the top-level.
Scenario 2: Do you have multiple cloud functions corresponding to different paths? Put x-google-backend at the operation-level and set x-google-backend.path_translation to APPEND_PATH_TO_ADDRESS.
When your invocation type is RequestRespone, you can access the payload directly from the event parameter of lambda.
Look at the `Payload' parameter of the Google function:
let params = {
FunctionName:'lambdafunction',
InvocationType: "RequestRespone",
Payload: JSON.stringify({ name: 'Arun'})
};
res.status(200).send(await lambda.invoke(params)...)
Also the Lambda part:
exports.handler = function(event, context) {
context.succeed('Hello ' + event.name);
};
I hope this helps.

How to Define Enum as Query Params in OpenAPI Spec

I want to define an enum value in the query string parameter of a function in an OpenAPI specification.
Here is an example of how I specify the parameter in my OpenAPI specification (yaml):
openapi: 3.0.0
info:
description: Vends sodas
version: 1.0.0
title: VendingMachineService
paths:
/soda/{bill}:
get:
summary: Provides a soda for a provided amount of money
description: Provides a soda for a provided amount of money
operationId: getSoda
parameters:
- name: bill
in: path
description: Money (e.g. one, two, five)
required: true
schema:
$ref: "#/components/schemas/Bill"
responses:
"200":
description: Success
content:
application/json:
schema:
$ref: "#/components/schemas/Result"
"404":
description: Templates for Source not found
servers:
- url: http://localhost:8080/
components:
schemas:
Bill:
type: string
enum:
- one
- two
- five
Result:
type: string
The key part of the OpenAPI spec is that I define the enum in a schema object at the bottom, type string, and refer to it in the parameters section of the function, also defining it as part of the path /{bill}
I then use openapi-generator to generate the spring server:
openapi-generator generate -i ~/vending-machine.yaml -g spring -o ~/output
I then spin up the server (importing the project into Intellij and running in a spring-boot configuration) and go to localhost:8080/ to open the Swagger UI. I try to exercise the api and get the following error:
{ "timestamp": "2019-03-29T15:43:14.737Z", "status": 400,
"error": "Bad Request", "message": "Failed to convert value of type
'java.lang.String' to required type 'org.openapitools.model.Bill';
nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type [java.lang.String] to type
[#javax.validation.constraints.NotNull
#io.swagger.annotations.ApiParam #javax.validation.Valid
#org.springframework.web.bind.annotation.RequestParam
org.openapitools.model.Bill] for value 'null'; nested exception is
java.lang.IllegalArgumentException: No enum constant
org.openapitools.model.Bill.null", "path": "/soda/%7Bbill%7D" }
(501 Not Implemented expected)
What is the proper way to define the query params & enum in the OpenAPI spec to use enum values?

Resources