Replies: 1 comment 3 replies
-
@MCR2019 Hi! I simplified the problem and implemented it based on the expectation that when I see: return {"postTown": "test value"} Here is my code. hello_world/app.py from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.utilities.data_classes import (
APIGatewayProxyEvent,
event_source,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from pydantic import BaseModel
from pydantic.alias_generators import to_camel
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(path="/swagger")
class User(BaseModel):
class Config:
alias_generator = to_camel
populate_by_name = True
post_town: str
# return : {"postTown": "Tokyo"}, status:200 when you hit the endpoint
@app.get("/user") # mod endpoint to /user
def get() -> User:
return User(**{"post_town": "Tokyo"})
@event_source(data_class=APIGatewayProxyEvent)
def lambda_handler(event: APIGatewayProxyEvent, context: LambdaContext):
return app.resolve(event.raw_event, context) template.yml AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
discussion#4507
Powertools example
Globals: # https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html
Function:
Timeout: 5
MemorySize: 128
Runtime: python3.11
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
Handler: app.lambda_handler
CodeUri: hello_world
Description: Hello World function
Architectures:
- x86_64
Tracing: Active
Events:
HelloPath:
Type: Api # More info about API Event Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html
Properties:
Path: /{proxy+} # FIXED PROXY ENDPOINT
Method: any # ANY HTTP METHOD
# Powertools env vars: https://awslabs.github.io/aws-lambda-powertools-python/#environment-variables
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: PowertoolsHelloWorld
POWERTOOLS_METRICS_NAMESPACE: Powertools
LOG_LEVEL: INFO
Tags:
LambdaPowertools: python
Outputs:
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod environment for Hello World Function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn Usagesam build
sam local start-api
# Please access http://localhost:3000/swagger using a browser. This is endpoint generated swagger doc. Does this answer the question? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
There a good chance that I'm not doing things in the correct way here but interested in other peoples thoughts on the following.
I'm using an
APIGatewayRestResolver
withenable_validation=True
and specifying the return type as aPydantic
model.The
Pydantic
model has aField
where I setalias
as well asserialization_alias
. So the field in the response has the key specified byseriliazation_alias
as I'd expect.Where I generate an openAPI spec though, the response in the schema has the
alias
value.Hopefully this example code make this clearer:
In the above code the response body is:
{"post_town": "test value"}
however in the generated openAPI spec the response schema is:Any help would be great!
Beta Was this translation helpful? Give feedback.
All reactions