ab-openapi-python-generator 2.2.5__py3-none-any.whl → 2.2.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -144,38 +144,46 @@ HTTP_OPERATIONS = ["get", "post", "put", "delete", "options", "head", "patch", "
144
144
  def generate_body_param(operation: Operation) -> Union[str, None]:
145
145
  if operation.requestBody is None:
146
146
  return None
147
- else:
148
- if isinstance(operation.requestBody, Reference30) or isinstance(operation.requestBody, Reference31):
149
- return "data.dict()"
150
-
151
- if operation.requestBody.content is None:
152
- return None # pragma: no cover
153
-
154
- if operation.requestBody.content.get("application/json") is None:
155
- return None # pragma: no cover
156
-
157
- media_type = operation.requestBody.content.get("application/json")
158
-
159
- if media_type is None:
160
- return None # pragma: no cover
161
-
162
- if isinstance(media_type.media_type_schema, (Reference, Reference30, Reference31)):
163
- return "data.dict()"
164
- elif hasattr(media_type.media_type_schema, "ref"):
165
- # Handle Reference objects from different OpenAPI versions
166
- return "data.dict()"
167
- elif isinstance(media_type.media_type_schema, (Schema, Schema30, Schema31)):
168
- schema = media_type.media_type_schema
169
- if schema.type == "array":
170
- return "[i.dict() for i in data]"
171
- elif schema.type == "object":
172
- return "data"
173
- else:
174
- raise Exception(f"Unsupported schema type for request body: {schema.type}") # pragma: no cover
175
- else:
176
- raise Exception(
177
- f"Unsupported schema type for request body: {type(media_type.media_type_schema)}"
178
- ) # pragma: no cover
147
+
148
+ # If requestBody is a $ref, it will be a Pydantic model instance in the client.
149
+ if isinstance(operation.requestBody, (Reference30, Reference31)):
150
+ return "data.model_dump(by_alias=True, exclude_none=True)"
151
+
152
+ rb_content = getattr(operation.requestBody, "content", None)
153
+ if rb_content is None:
154
+ return None # pragma: no cover
155
+
156
+ if rb_content.get("application/json") is None:
157
+ return None # pragma: no cover
158
+
159
+ media_type = rb_content.get("application/json")
160
+ if media_type is None:
161
+ return None # pragma: no cover
162
+
163
+ mts = getattr(media_type, "media_type_schema", None)
164
+ if mts is None:
165
+ return None # pragma: no cover
166
+
167
+ # $ref schema -> model
168
+ if isinstance(mts, (Reference, Reference30, Reference31)) or hasattr(mts, "ref"):
169
+ return "data.model_dump(by_alias=True, exclude_none=True)"
170
+
171
+ # Concrete schema
172
+ if isinstance(mts, (Schema, Schema30, Schema31)):
173
+ schema = mts
174
+
175
+ if schema.type == "array":
176
+ # List of models or primitives
177
+ return "[i.model_dump(by_alias=True, exclude_none=True) if hasattr(i, 'model_dump') else i for i in data]"
178
+
179
+ if schema.type == "object":
180
+ # Model or dict-like
181
+ return "data.model_dump(by_alias=True, exclude_none=True) if hasattr(data, 'model_dump') else data"
182
+
183
+ # Primitive (string/int/etc.)
184
+ return "data"
185
+
186
+ raise Exception(f"Unsupported schema type for request body: {type(mts)}") # pragma: no cover
179
187
 
180
188
 
181
189
  def generate_params(operation: Operation) -> str:
@@ -13,6 +13,7 @@ from ab_openapi_python_generator.language_converters.python.exception_generator
13
13
  )
14
14
  from ab_openapi_python_generator.language_converters.python.model_generator import (
15
15
  generate_models,
16
+ generate_response_union_alias_models,
16
17
  )
17
18
  from ab_openapi_python_generator.models import ConversionResult, LibraryConfig
18
19
 
@@ -40,6 +41,14 @@ def generator(
40
41
  else:
41
42
  models = []
42
43
 
44
+ # Generate response union alias models (e.g. AuthorizeResponse) from paths
45
+ # so client return types that reference those aliases have corresponding
46
+ # model modules available in the output.
47
+ if data.paths is not None:
48
+ resp_alias_models = generate_response_union_alias_models(data.paths, pydantic_version)
49
+ if resp_alias_models:
50
+ models.extend(resp_alias_models)
51
+
43
52
  if data.paths is not None:
44
53
  clients = generate_clients(data, data.paths, library_config, env_token_name, pydantic_version)
45
54
  else:
@@ -757,16 +757,21 @@ def _generate_property_from_schema(
757
757
  """
758
758
  required = parent_schema is not None and parent_schema.required is not None and name in parent_schema.required
759
759
 
760
- import_type = None
760
+ # Pick up OpenAPI default (if present)
761
+ default_val = getattr(schema, "default", None)
762
+
761
763
  if required:
762
- import_type = [] if name == model_name else [name]
764
+ rendered_default = None
765
+ else:
766
+ # If OpenAPI provides a default, use it. Otherwise fall back to None.
767
+ rendered_default = repr(default_val) if default_val is not None else "None"
763
768
 
764
769
  return Property(
765
770
  name=name,
766
771
  type=type_converter(schema, required, model_name),
767
772
  required=required,
768
- default=None if required else "None",
769
- import_type=import_type,
773
+ default=rendered_default,
774
+ import_type=None if not required else ([] if name == model_name else [name]),
770
775
  )
771
776
 
772
777
 
@@ -57,15 +57,20 @@ AsyncGenerator[str | dict[str, Any], None]
57
57
  path = f"{{ op.path_name }}"
58
58
 
59
59
  headers = {
60
+ {% if op.body_param %}
60
61
  "Content-Type": "application/json",
62
+ {% endif %}
61
63
  {% if op.is_sse %}
62
64
  "Accept": "text/event-stream",
63
65
  {% else %}
64
66
  "Accept": "application/json",
65
67
  {% endif %}
66
- "Authorization": f"Bearer { await self.get_access_token() }",
67
68
  }
68
69
 
70
+ _token = await self.get_access_token()
71
+ if _token:
72
+ headers["Authorization"] = f"Bearer {_token}"
73
+
69
74
  query_params: Dict[str, Any] = {
70
75
  {% for qp in op.query_params %}
71
76
  {{ qp }},
@@ -57,15 +57,20 @@ Generator[str | dict[str, Any], None, None]
57
57
  path = f"{{ op.path_name }}"
58
58
 
59
59
  headers = {
60
+ {% if op.body_param %}
60
61
  "Content-Type": "application/json",
62
+ {% endif %}
61
63
  {% if op.is_sse %}
62
64
  "Accept": "text/event-stream",
63
65
  {% else %}
64
66
  "Accept": "application/json",
65
67
  {% endif %}
66
- "Authorization": f"Bearer { self.get_access_token() }",
67
68
  }
68
69
 
70
+ _token = self.get_access_token()
71
+ if _token:
72
+ headers["Authorization"] = f"Bearer {_token}"
73
+
69
74
  query_params: Dict[str, Any] = {
70
75
  {% for qp in op.query_params %}
71
76
  {{ qp }},
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ab-openapi-python-generator
3
- Version: 2.2.5
3
+ Version: 2.2.7
4
4
  Summary: Openapi Python Generator
5
5
  Project-URL: Homepage, https://github.com/auth-broker/openapi-python-generator
6
6
  Project-URL: Repository, https://github.com/auth-broker/openapi-python-generator
@@ -7,25 +7,25 @@ ab_openapi_python_generator/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZ
7
7
  ab_openapi_python_generator/version_detector.py,sha256=PUDTpgDMHljUzxhLhMrCzHMdS3mgYQibWDiI4-vLRB4,2104
8
8
  ab_openapi_python_generator/language_converters/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  ab_openapi_python_generator/language_converters/python/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
- ab_openapi_python_generator/language_converters/python/client_generator.py,sha256=7KB1PLRnbqh623RcvwVMoK1mhdwXmZGn6K2Sm3-nSBE,19306
10
+ ab_openapi_python_generator/language_converters/python/client_generator.py,sha256=j_2AIR9YZmXH8si23Jety5vrsuq--afwHT9pQATN_Qw,19402
11
11
  ab_openapi_python_generator/language_converters/python/common.py,sha256=gZHNhVC4OwGBlmr3h9Ffz3iILO1ykva9CrqhBfeYQXU,1616
12
12
  ab_openapi_python_generator/language_converters/python/exception_generator.py,sha256=a4iX53W8qhQ99Tkdk2sHPCq3HvtZp5JctTbObgsRX7I,653
13
- ab_openapi_python_generator/language_converters/python/generator.py,sha256=g-WTVLidcpDLqYW00VufmK0_jfRAM1ZVOSAcHyLAhOU,1664
13
+ ab_openapi_python_generator/language_converters/python/generator.py,sha256=AnIiTZm0ZHd07-XSvLrqBnS-gn2vaO8AD1QywW1VlmM,2109
14
14
  ab_openapi_python_generator/language_converters/python/jinja_config.py,sha256=B-qSmxfrplroItvHLS7fp-FC_PkroJLT9fsB5-9J0oI,1267
15
- ab_openapi_python_generator/language_converters/python/model_generator.py,sha256=lx-SFNgYGGNAPS6yzZFaK21_qxWJ_XC4VzEETvrxFFY,42292
15
+ ab_openapi_python_generator/language_converters/python/model_generator.py,sha256=rzOVF80iK3ekB9FbFNnTjBFgber6B5C96u-Uai_VHDw,42550
16
16
  ab_openapi_python_generator/language_converters/python/templates/alias_union.jinja2,sha256=tndnrDky4srNRyB_HnUMZFW7HtoJvEfwIFteYIu0ELc,468
17
- ab_openapi_python_generator/language_converters/python/templates/async_client_httpx_pydantic_2.jinja2,sha256=6loXkLatHYrqMWxWyUr9_PWb73jJbUetS-67-YoOK9Q,4931
17
+ ab_openapi_python_generator/language_converters/python/templates/async_client_httpx_pydantic_2.jinja2,sha256=GOJWwbALdHwTnj3UUB6O5kPBqCHJNiG_WzukJtXpcsI,5017
18
18
  ab_openapi_python_generator/language_converters/python/templates/discriminator_enum.jinja2,sha256=GJZ_ih1eURnJRPZq5P_3aKhYB5iJHymj2fpuNSdM90Y,163
19
19
  ab_openapi_python_generator/language_converters/python/templates/enum.jinja2,sha256=7dewyMLifUTe9xjCTE62deuncPiNkp7eGkIaY-IWsLo,263
20
20
  ab_openapi_python_generator/language_converters/python/templates/http_exception.jinja2,sha256=BHy48PD74aU74NGWC98iciacmGotHuWw9CaOqnCL8Kc,294
21
21
  ab_openapi_python_generator/language_converters/python/templates/models.jinja2,sha256=s5PllbxiEENsZ5_fKSh1gKMJWhcLfBdH5744nKnAZsQ,797
22
22
  ab_openapi_python_generator/language_converters/python/templates/models_pydantic_2.jinja2,sha256=o6-OlV5upoZOAVx6aQbsaigPMTCi4Uuec-oP1FNDJ60,904
23
- ab_openapi_python_generator/language_converters/python/templates/sync_client_httpx_pydantic_2.jinja2,sha256=Cv1XUM99atLNlj0rWyEqi5jWTKhDPJRVp5-xfuEn4Ko,4776
23
+ ab_openapi_python_generator/language_converters/python/templates/sync_client_httpx_pydantic_2.jinja2,sha256=MaFsVuKq5K0rEldlc_HZEU98MdFCXSEKFznxJHKMl3U,4862
24
24
  ab_openapi_python_generator/parsers/__init__.py,sha256=UITDIV7rp3HAA1M8kQc_9cNG6e1tS2vDa4mTKua6aMY,300
25
25
  ab_openapi_python_generator/parsers/openapi_30.py,sha256=2Hq5Vl3V4NUHKol-OO2nmt8-Fh6Lqk-hJUys8s-4UNI,1924
26
26
  ab_openapi_python_generator/parsers/openapi_31.py,sha256=jyKYAKDRkqKUPSeP6xZuM6ZCE0JjuowAe_pbm01iqWE,1924
27
- ab_openapi_python_generator-2.2.5.dist-info/METADATA,sha256=x2g50ud0ktO1p0bPiqIt_J0jqZBZQzAP3kzQpbbOEHw,5186
28
- ab_openapi_python_generator-2.2.5.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
29
- ab_openapi_python_generator-2.2.5.dist-info/entry_points.txt,sha256=zezbCs2qP8lSUbcjL6cN5XHoHxf9JTe15CZYH8Yuzo0,90
30
- ab_openapi_python_generator-2.2.5.dist-info/licenses/LICENSE,sha256=0myanGwJ2vUOZN12aN95o0My6XEysnnVlbKikYw3pHg,1070
31
- ab_openapi_python_generator-2.2.5.dist-info/RECORD,,
27
+ ab_openapi_python_generator-2.2.7.dist-info/METADATA,sha256=g9Wn_Yob0tnCOs5KMiutnQziyqyWUcB5LY3eigSAWJQ,5186
28
+ ab_openapi_python_generator-2.2.7.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
29
+ ab_openapi_python_generator-2.2.7.dist-info/entry_points.txt,sha256=zezbCs2qP8lSUbcjL6cN5XHoHxf9JTe15CZYH8Yuzo0,90
30
+ ab_openapi_python_generator-2.2.7.dist-info/licenses/LICENSE,sha256=0myanGwJ2vUOZN12aN95o0My6XEysnnVlbKikYw3pHg,1070
31
+ ab_openapi_python_generator-2.2.7.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.28.0
2
+ Generator: hatchling 1.29.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any