From 0f4671f3fc42b9a232b0ca730251cd2d2411fcd6 Mon Sep 17 00:00:00 2001 From: dphuang2 Date: Tue, 31 Oct 2023 17:22:30 -0700 Subject: [PATCH] test working for pydantic responses --- .../main/resources/python/endpoint.handlebars | 12 +++++- .../resources/python/endpoint_args.handlebars | 5 +++ .../python/endpoint_pydantic_impl.handlebars | 19 +++++++++ .../main/resources/python/pydantic.handlebars | 4 +- .../python/pydantic_templates/type.handlebars | 12 +++--- .../pydantic_templates/type_object.handlebars | 42 ++----------------- 6 files changed, 46 insertions(+), 48 deletions(-) create mode 100644 generator/konfig-generator-api/src/main/resources/python/endpoint_pydantic_impl.handlebars diff --git a/generator/konfig-generator-api/src/main/resources/python/endpoint.handlebars b/generator/konfig-generator-api/src/main/resources/python/endpoint.handlebars index c0d8313754..f14a05cc52 100644 --- a/generator/konfig-generator-api/src/main/resources/python/endpoint.handlebars +++ b/generator/konfig-generator-api/src/main/resources/python/endpoint.handlebars @@ -219,10 +219,20 @@ class BaseApi(api_client.Api): {{> endpoint_operation_impl}} -class {{operationIdCamelCase}}(BaseApi): +class {{#prstv2}}Raw{{/prstv2}}{{operationIdCamelCase}}(BaseApi): # this class is used by api classes that refer to endpoints with operationId fn names {{> endpoint_interface_impl methodName=operationId}} +{{#prstv2}} + + +class {{operationIdCamelCase}}(BaseApi): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.raw = Raw{{operationIdCamelCase}}(*args, **kwargs) + + {{> endpoint_pydantic_impl}} +{{/prstv2}} class ApiFor{{httpMethod}}(BaseApi): diff --git a/generator/konfig-generator-api/src/main/resources/python/endpoint_args.handlebars b/generator/konfig-generator-api/src/main/resources/python/endpoint_args.handlebars index 1e51b9508f..14358bea82 100644 --- a/generator/konfig-generator-api/src/main/resources/python/endpoint_args.handlebars +++ b/generator/konfig-generator-api/src/main/resources/python/endpoint_args.handlebars @@ -77,6 +77,11 @@ {{/if}} stream: bool = False, {{/if}} +{{#if interface}} +{{#if validateArg}} + validate: bool = False, +{{/if}} +{{/if}} ){{#unless omitReturnType}} -> {{#if isMappedArgs}}api_client.MappedArgs{{else}}typing.Union[ {{#each responses}} {{#if isDefault}} diff --git a/generator/konfig-generator-api/src/main/resources/python/endpoint_pydantic_impl.handlebars b/generator/konfig-generator-api/src/main/resources/python/endpoint_pydantic_impl.handlebars new file mode 100644 index 0000000000..71941c8bd7 --- /dev/null +++ b/generator/konfig-generator-api/src/main/resources/python/endpoint_pydantic_impl.handlebars @@ -0,0 +1,19 @@ +{{#if isDeprecated}}@api_client.DeprecationWarningOnce(prefix="{{clientApiName}}") +{{/if}}async def a{{operationId}}( +{{> endpoint_args async=true isOverload=false skipDeserialization="null" contentType="null" interface=true omitReturnType=true validateArg=true}} + raw_response = await self.raw.a{{operationId}}( + {{> endpoint_args_call_mapped_args}} + ) + if validate: + return {{returnType}}Pydantic(**raw_response.body) + return {{returnType}}Pydantic.model_construct(**raw_response.body) + +{{#if isDeprecated}}@api_client.DeprecationWarningOnce(prefix="{{clientApiName}}") +{{/if}}def {{operationId}}( +{{> endpoint_args isOverload=false skipDeserialization="null" contentType="null" interface=true omitReturnType=true validateArg=true}} + raw_response = self.raw.{{operationId}}( + {{> endpoint_args_call_mapped_args}} + ) + if validate: + return {{returnType}}Pydantic(**raw_response.body) + return {{returnType}}Pydantic.model_construct(**raw_response.body) diff --git a/generator/konfig-generator-api/src/main/resources/python/pydantic.handlebars b/generator/konfig-generator-api/src/main/resources/python/pydantic.handlebars index 43e7de45d1..62978fd53e 100644 --- a/generator/konfig-generator-api/src/main/resources/python/pydantic.handlebars +++ b/generator/konfig-generator-api/src/main/resources/python/pydantic.handlebars @@ -3,9 +3,7 @@ {{>partial_header}} from datetime import datetime, date -import typing -from enum import Enum -from typing_extensions import TypedDict, Literal +from pydantic import BaseModel, Field {{#each models}} {{#with model}} diff --git a/generator/konfig-generator-api/src/main/resources/python/pydantic_templates/type.handlebars b/generator/konfig-generator-api/src/main/resources/python/pydantic_templates/type.handlebars index dcd00ad741..a5ebbdab9e 100644 --- a/generator/konfig-generator-api/src/main/resources/python/pydantic_templates/type.handlebars +++ b/generator/konfig-generator-api/src/main/resources/python/pydantic_templates/type.handlebars @@ -1,6 +1,6 @@ -{{> type_templates/type_object}} -{{> type_templates/type_composed}} -{{> type_templates/type_enum}} -{{> type_templates/type_list}} -{{> type_templates/type_map}} -{{> type_templates/type_primitive}} \ No newline at end of file +{{> pydantic_templates/type_object}} +{{> pydantic_templates/type_composed}} +{{> pydantic_templates/type_enum}} +{{> pydantic_templates/type_list}} +{{> pydantic_templates/type_map}} +{{> pydantic_templates/type_primitive}} \ No newline at end of file diff --git a/generator/konfig-generator-api/src/main/resources/python/pydantic_templates/type_object.handlebars b/generator/konfig-generator-api/src/main/resources/python/pydantic_templates/type_object.handlebars index b299a6e621..836c5c7881 100644 --- a/generator/konfig-generator-api/src/main/resources/python/pydantic_templates/type_object.handlebars +++ b/generator/konfig-generator-api/src/main/resources/python/pydantic_templates/type_object.handlebars @@ -1,18 +1,5 @@ {{#if hasVars}} -{{#if useDictionaryBasedTypedDict}} -Required{{classname}} = TypedDict("Required{{classname}}", { - {{#each requiredVars}} - {{#if description}} - # {{{description}}} - {{/if}} - "{{baseName}}": {{dataType}}, - {{#unless @last}} - - {{/unless}} - {{/each}} - }) -{{else}} -class Required{{classname}}(TypedDict): +class {{classname}}(BaseModel): {{#each requiredVars}} {{#if description}} # {{{description}}} @@ -22,39 +9,18 @@ class Required{{classname}}(TypedDict): {{/unless}} {{/each}} - {{#unless requiredVars}} - pass - {{/unless}} -{{/if}} - -{{#if useDictionaryBasedTypedDict}} -Optional{{classname}} = TypedDict("Optional{{classname}}", { {{#each optionalVars}} {{#if description}} # {{{description}}} {{/if}} - "{{baseName}}": {{dataType}}, - {{#unless @last}} - - {{/unless}} - {{/each}} - }, total=False) -{{else}} -class Optional{{classname}}(TypedDict, total=False): - {{#each optionalVars}} - {{#if description}} - # {{{description}}} - {{/if}} - {{baseName}}: {{{dataType}}} + {{baseName}}: {{{dataType}}} = Field(None) {{#unless @last}} {{/unless}} {{/each}} + {{#unless requiredVars}} {{#unless optionalVars}} pass {{/unless}} -{{/if}} - -class {{classname}}(Required{{classname}}, Optional{{classname}}): - pass + {{/unless}} {{/if}}