Skip to content

Commit

Permalink
fix(type-safe-api): fix syntax error for python runtime request param…
Browse files Browse the repository at this point in the history
…eters with descriptions

Descriptions in Python request parameters would previously render invalid python syntax as quotes
were escaped as ". Use triple curly braces to ensure these are rendered correctly.
Additionally, field references can only be used once per field in a pydantic model, and since fields
with descriptions include the field in the type annotation these would fail at runtime when another
field is used in the assignment (ie foo: Annotated[StrictStr, Field(..., description="...")] =
Field(alias='foo') ). We therefore remove the assignment of a Field value, which is ok since the
alias is not required as the handler wrapper already constructs a dict with the "paramName"s as keys
rather than the "baseName"s which are in the raw request.

Fixes #609
  • Loading branch information
cogwirrel committed Oct 20, 2023
1 parent c922390 commit c2a3f33
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
import urllib.parse
import json
from typing import Callable, Any, Dict, List, NamedTuple, TypeVar, Generic, Union, TypedDict, Protocol, Optional, Literal
from typing import Callable, Any, Dict, List, NamedTuple, TypeVar, Generic, Union, TypedDict, Protocol, Optional, Literal, Annotated
from functools import wraps
from dataclasses import dataclass, fields
from datetime import datetime
Expand Down Expand Up @@ -215,7 +215,7 @@ class {{operationIdCamelCase}}RequestParameters(BaseModel):
"""
{{#allParams}}
{{^isBodyParam}}
{{paramName}}: {{vendorExtensions.x-py-typing}} = Field(alias='{{baseName}}'{{^required}}, default=None{{/required}})
{{paramName}}: {{{vendorExtensions.x-py-typing}}}
{{/isBodyParam}}
{{/allParams}}
Expand All @@ -232,7 +232,7 @@ class {{operationIdCamelCase}}RequestParameters(BaseModel):
return cls.from_dict(json.loads(json_str))
def to_dict(self):
return self.dict(by_alias=True, exclude={}, exclude_none=True)
return self.dict(exclude={}, exclude_none=True)
@classmethod
def from_dict(cls, obj: dict) -> {{operationIdCamelCase}}RequestParameters:
Expand Down
6 changes: 6 additions & 0 deletions packages/type-safe-api/test/resources/specs/single.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ paths:
parameters:
- in: query
name: param1
description: This is parameter 1
schema:
type: string
required: true
- in: query
name: param2
description: This is parameter 2
schema:
type: array
items:
Expand All @@ -36,6 +38,7 @@ paths:
required: true
- in: header
name: x-header-param
description: This is a header parameter
schema:
type: string
required: true
Expand Down Expand Up @@ -149,11 +152,13 @@ components:
required:
- errorMessage
TestRequest:
description: This is a test request
type: object
properties:
myInput:
type: number
TestResponse:
description: This is a test response
type: object
properties:
messages:
Expand All @@ -162,6 +167,7 @@ components:
type: object
properties:
message:
description: This is a message
type: string
id:
type: integer
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c2a3f33

Please sign in to comment.