api-foundry 0.1.20__tar.gz → 0.1.30__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: api-foundry
3
- Version: 0.1.20
3
+ Version: 0.1.30
4
4
  Summary: The `api_foundry` project is a powerful tool designed to automate the deployment of REST APIs on AWS using Lambda services to access and interact with relational databases (RDBMS). This project leverages the OpenAPI specification to define and manage the APIs
5
5
  Project-URL: Documentation, https://github.com/DanRepik/api-foundry
6
6
  Project-URL: Source, https://github.com/DanRepik/api-foundry
@@ -83,9 +83,7 @@ def load_api_spec(api_spec: Union[str, list[str]]) -> dict:
83
83
  try:
84
84
  as_yaml = yaml.safe_load(spec)
85
85
  except yaml.YAMLError as exc: # not YAML
86
- raise ValueError(
87
- f"Invalid OpenAPI spec source: {spec}"
88
- ) from exc
86
+ raise ValueError(f"Invalid OpenAPI spec source: {spec}") from exc
89
87
 
90
88
  if is_valid_openapi_spec(as_yaml):
91
89
  inputs.append(as_yaml)
@@ -124,9 +122,7 @@ class APIFoundry(ComponentResource):
124
122
  *,
125
123
  api_spec: Union[str, list[str]],
126
124
  secrets: Optional[str] = None,
127
- environment: Optional[
128
- dict[str, Union[str, pulumi.Output[str]]]
129
- ] = None,
125
+ environment: Optional[dict[str, Union[str, pulumi.Output[str]]]] = None,
130
126
  integrations: Optional[list[dict]] = None,
131
127
  token_validators: Optional[list[dict]] = None,
132
128
  policy_statements: Optional[list] = None,
@@ -154,7 +150,7 @@ class APIFoundry(ComponentResource):
154
150
 
155
151
  env_vars["SECRETS"] = secrets
156
152
 
157
- # Grant read access to referenced secrets (single broad stmt)
153
+ # Grant read access to referenced secrets (single broad stmt)
158
154
  if json.loads(secrets):
159
155
  policy_statements.append(
160
156
  {
@@ -26,8 +26,10 @@ SUPPORTED_TYPES = {
26
26
  "date",
27
27
  "date-time",
28
28
  "time",
29
- # Note: OpenAPI uses type 'number' with format 'float'/'double'.
30
- # Types 'float' and 'double' are intentionally NOT supported here.
29
+ # Note: OpenAPI uses base type 'number' with optional
30
+ # format 'float'/'double'. These formats are accepted and
31
+ # normalized to api_type='number' while preserving the
32
+ # original format in a separate attribute for mapping.
31
33
  "uuid",
32
34
  "array",
33
35
  "object",
@@ -35,10 +37,16 @@ SUPPORTED_TYPES = {
35
37
 
36
38
 
37
39
  class OpenAPIElement:
38
- """Base class for OpenAPI elements like schema properties and associations."""
40
+ """
41
+ Base class for OpenAPI elements like schema properties and
42
+ associations.
43
+ """
39
44
 
40
45
  def to_dict(self) -> Dict[str, Any]:
41
- """Converts the OpenAPI element to a dictionary, including nested properties."""
46
+ """
47
+ Convert the OpenAPI element to a dictionary, including nested
48
+ properties.
49
+ """
42
50
  return {
43
51
  k: (v.to_dict() if isinstance(v, OpenAPIElement) else v)
44
52
  for k, v in self.__dict__.items()
@@ -47,13 +55,27 @@ class OpenAPIElement:
47
55
 
48
56
 
49
57
  class SchemaObjectProperty(OpenAPIElement):
50
- """Represents a property of a schema object in the OpenAPI specification."""
58
+ """
59
+ Represents a property of a schema object in the OpenAPI
60
+ specification.
61
+ """
51
62
 
52
63
  def __init__(self, schema_name: str, name: str, prop: Dict[str, Any]):
53
64
  super().__init__()
54
65
  self.api_name = name
55
- # Prefer explicit OpenAPI format when present, else fall back to type.
56
- self.api_type = prop.get("format") or prop.get("type") or "string"
66
+ # Base OpenAPI type and format
67
+ base_type = prop.get("type") or "string"
68
+ api_format = prop.get("format")
69
+ # Preserve numeric formats (float/double) but normalize api_type
70
+ # to 'number'
71
+ if base_type == "number" and api_format in {"float", "double"}:
72
+ self.api_type = "number"
73
+ self.numeric_format = api_format
74
+ else:
75
+ # Maintain prior behavior for date/date-time/time/uuid by
76
+ # allowing format as type
77
+ self.api_type = api_format or base_type
78
+ self.numeric_format = None
57
79
  # Validate the raw OpenAPI 'type' first to catch invalid base types
58
80
  raw_type = prop.get("type")
59
81
  if raw_type is not None and raw_type not in {
@@ -67,17 +89,33 @@ class SchemaObjectProperty(OpenAPIElement):
67
89
  raise ApplicationException(
68
90
  500,
69
91
  (
70
- f"Property: {name} in schema object: {schema_name} of type: "
71
- + f"{raw_type} is not a valid type"
92
+ f"Property: {name} in schema object: {schema_name} "
93
+ f"of type: {raw_type} is not a valid type"
72
94
  ),
73
95
  )
74
96
  if self.api_type not in SUPPORTED_TYPES:
75
97
  raise ApplicationException(
76
98
  500,
77
- f"Property: {name} in schema object: {schema_name} of type: {self.api_type} is not a valid type",
99
+ (
100
+ f"Property: {name} in schema object: {schema_name} "
101
+ f"of type: {self.api_type} is not a valid type"
102
+ ),
78
103
  )
79
104
  self.column_name = prop.get("x-af-column-name") or name
80
- self.column_type = prop.get("x-af-column-type") or prop.get("type") or "string"
105
+ # Choose column type. If user didn't specify x-af-column-type and this
106
+ # is a numeric with a float/double format, apply a sensible default
107
+ # (Postgres: float -> real, double -> double precision).
108
+ if prop.get("x-af-column-type"):
109
+ self.column_type = prop.get("x-af-column-type")
110
+ elif base_type == "number" and self.numeric_format in {
111
+ "float",
112
+ "double",
113
+ }:
114
+ self.column_type = (
115
+ "real" if self.numeric_format == "float" else "double precision"
116
+ )
117
+ else:
118
+ self.column_type = prop.get("type") or "string"
81
119
  self.required = prop.get("required", False)
82
120
  self.min_length = prop.get("minLength", None)
83
121
  self.max_length = prop.get("maxLength", None)
@@ -187,7 +225,8 @@ class SchemaObject(OpenAPIElement):
187
225
  def _resolve_property(
188
226
  self, property_name: str, prop: Dict[str, Any]
189
227
  ) -> Optional[SchemaObjectProperty]:
190
- # Validate raw OpenAPI base type early to catch invalid types like 'float'
228
+ # Validate raw OpenAPI base type early to catch invalid types
229
+ # like 'float'
191
230
  raw_type = prop.get("type")
192
231
  if raw_type is not None and raw_type not in {
193
232
  "string",
@@ -200,8 +239,8 @@ class SchemaObject(OpenAPIElement):
200
239
  raise ApplicationException(
201
240
  500,
202
241
  (
203
- f"Property: {property_name} in schema object: {self.api_name} "
204
- + f"of type: {raw_type} is not a valid type"
242
+ f"Property: {property_name} in schema object: "
243
+ f"{self.api_name} of type: {raw_type} is not a valid type"
205
244
  ),
206
245
  )
207
246
  # If $ref is present, treat as relation
@@ -270,34 +309,44 @@ class SchemaObject(OpenAPIElement):
270
309
  ):
271
310
  raise ApplicationException(
272
311
  500,
273
- f"Invalid concurrency property: {concurrency_property_name} not found in properties.",
312
+ (
313
+ "Invalid concurrency property: "
314
+ f"{concurrency_property_name} not found in properties."
315
+ ),
274
316
  )
275
317
  return concurrency_property_name
276
318
 
277
319
  def _get_primary_key(self, schema_object: dict) -> Optional[str]:
278
320
  for property_name, properties in schema_object.get("properties", {}).items():
279
321
  if "x-af-primary-key" in properties:
280
- property = self.properties[property_name]
281
- property.key_type = properties.get("x-af-primary-key", "auto")
282
-
283
- if property.key_type not in ["manual", "uuid", "auto", "sequence"]:
322
+ prop_obj = self.properties[property_name]
323
+ prop_obj.key_type = properties.get("x-af-primary-key", "auto")
324
+
325
+ if prop_obj.key_type not in [
326
+ "manual",
327
+ "uuid",
328
+ "auto",
329
+ "sequence",
330
+ ]:
284
331
  raise ApplicationException(
285
332
  500,
286
333
  (
287
- f"Invalid primary key type '{property.key_type}' "
334
+ f"Invalid primary key type '{prop_obj.key_type}' "
288
335
  + f"in schema object '{self.api_name}'"
289
336
  + f", property '{property_name}'"
290
337
  ),
291
338
  )
292
339
 
293
- if property.key_type == "sequence":
294
- property.sequence_name = properties.get("x-af-sequence-name", None)
295
- if not property.sequence_name:
340
+ if prop_obj.key_type == "sequence":
341
+ prop_obj.sequence_name = properties.get("x-af-sequence-name", None)
342
+ if not prop_obj.sequence_name:
296
343
  raise ApplicationException(
297
344
  500,
298
345
  (
299
- "Sequence-based primary keys must have a sequence name "
300
- + f"in schema object '{self.api_name}', property '{property_name}'"
346
+ "Sequence-based primary keys must have a "
347
+ "sequence name "
348
+ + f"in schema object '{self.api_name}', "
349
+ + f"property '{property_name}'"
301
350
  ),
302
351
  )
303
352
  return property_name
@@ -325,11 +374,13 @@ class SchemaObject(OpenAPIElement):
325
374
  if isinstance(v, dict):
326
375
  # Any dict that looks like action->rule (value not dict)
327
376
  for k2, v2 in v.items():
328
- if (
329
- k2
330
- in {"read", "write", "delete", "create", "update"}
331
- and not isinstance(v2, dict)
332
- ):
377
+ if k2 in {
378
+ "read",
379
+ "write",
380
+ "delete",
381
+ "create",
382
+ "update",
383
+ } and not isinstance(v2, dict):
333
384
  return True
334
385
  return False
335
386
 
@@ -359,9 +410,7 @@ class SchemaObject(OpenAPIElement):
359
410
  def to_dict(self) -> Dict[str, Any]:
360
411
  """Recursively convert this schema object and its properties."""
361
412
  data = super().to_dict()
362
- data["properties"] = {
363
- k: v.to_dict() for k, v in self.properties.items()
364
- }
413
+ data["properties"] = {k: v.to_dict() for k, v in self.properties.items()}
365
414
  data["relations"] = {k: v.to_dict() for k, v in self.relations.items()}
366
415
  if self.concurrency_property:
367
416
  data["concurrency_property"] = self.concurrency_property
@@ -369,7 +418,10 @@ class SchemaObject(OpenAPIElement):
369
418
 
370
419
 
371
420
  class PathOperation(OpenAPIElement):
372
- """Represents a single operation (method) on a path in the OpenAPI specification."""
421
+ """
422
+ Represents a single operation (method) on a path in the OpenAPI
423
+ specification.
424
+ """
373
425
 
374
426
  def __init__(self, path: str, method: str, path_operation: Dict[str, Any]):
375
427
  super().__init__()
@@ -404,9 +456,7 @@ class PathOperation(OpenAPIElement):
404
456
  for name, prop_schema in (
405
457
  path_operation.get("requestBody", {}).get("content", {}) or {}
406
458
  ).items():
407
- properties[name] = SchemaObjectProperty(
408
- self.entity, name, prop_schema
409
- )
459
+ properties[name] = SchemaObjectProperty(self.entity, name, prop_schema)
410
460
  elif section == "parameters":
411
461
  for param in path_operation.get("parameters", {}) or []:
412
462
  properties[param["name"]] = SchemaObjectProperty(
@@ -431,9 +481,10 @@ class PathOperation(OpenAPIElement):
431
481
  return properties
432
482
 
433
483
  def _get_permissions(self, path_operation: dict) -> dict:
434
- """Extract permissions from a path operation using x-af-permissions only.
435
-
436
- Also normalizes action names so 'create'/'update' map to 'write'.
484
+ """
485
+ Extract permissions from a path operation using x-af-permissions
486
+ only. Also normalizes action names so 'create'/'update' map to
487
+ 'write'.
437
488
  """
438
489
  raw_permissions = path_operation.get("x-af-permissions") or {}
439
490
 
@@ -450,11 +501,13 @@ class PathOperation(OpenAPIElement):
450
501
  for v in obj.values():
451
502
  if isinstance(v, dict):
452
503
  for k2, v2 in v.items():
453
- if (
454
- k2
455
- in {"read", "write", "delete", "create", "update"}
456
- and not isinstance(v2, dict)
457
- ):
504
+ if k2 in {
505
+ "read",
506
+ "write",
507
+ "delete",
508
+ "create",
509
+ "update",
510
+ } and not isinstance(v2, dict):
458
511
  return True
459
512
  return False
460
513
 
@@ -510,7 +563,10 @@ class ModelFactory:
510
563
  return merged
511
564
 
512
565
  def resolve_all_refs(self, spec: Dict[str, Any]) -> Dict[str, Any]:
513
- """Recursively resolve all $ref references in an OpenAPI specification."""
566
+ """
567
+ Recursively resolve all $ref references in an OpenAPI
568
+ specification.
569
+ """
514
570
 
515
571
  def resolve(obj: Any) -> Any:
516
572
  if isinstance(obj, dict):
@@ -555,11 +611,9 @@ class ModelFactory:
555
611
  log.info("path_operations: %s", self.path_operations)
556
612
  return {
557
613
  "schema_objects": {
558
- name: obj.to_dict()
559
- for name, obj in self.schema_objects.items()
614
+ name: obj.to_dict() for name, obj in self.schema_objects.items()
560
615
  },
561
616
  "path_operations": {
562
- name: obj.to_dict()
563
- for name, obj in self.path_operations.items()
617
+ name: obj.to_dict() for name, obj in self.path_operations.items()
564
618
  },
565
619
  }
@@ -36,7 +36,7 @@ def validate_permissions(permissions):
36
36
  def _validate_rule(action: str, role_name: str, rule):
37
37
  if action in {"read", "write"}:
38
38
  # Accept either a string regex or an object
39
- # {fields: regex, where?: string}
39
+ # {properties: regex, where?: string}
40
40
  if isinstance(rule, str):
41
41
  try:
42
42
  re.compile(rule)
@@ -52,7 +52,8 @@ def validate_permissions(permissions):
52
52
  + str(e)
53
53
  ) from e
54
54
  elif isinstance(rule, dict):
55
- allowed_keys = {"fields", "where"}
55
+ # Support both for backward compatibility
56
+ allowed_keys = {"properties", "fields", "where"}
56
57
  unknown = set(rule.keys()) - allowed_keys
57
58
  if unknown:
58
59
  raise ValueError(
@@ -60,18 +61,19 @@ def validate_permissions(permissions):
60
61
  f"'{role_name}' action '{action}'. "
61
62
  f"Allowed keys: {allowed_keys}."
62
63
  )
63
- fields = rule.get("fields")
64
- if not isinstance(fields, str):
64
+ # Accept both 'properties' (preferred) and 'fields' (legacy)
65
+ properties = rule.get("properties") or rule.get("fields")
66
+ if not isinstance(properties, str):
65
67
  raise ValueError(
66
68
  f"Rule for role '{role_name}' action "
67
- f"'{action}' must include 'fields' as a regex "
69
+ f"'{action}' must include 'properties' as a regex "
68
70
  f"string."
69
71
  )
70
72
  try:
71
- re.compile(fields)
73
+ re.compile(properties)
72
74
  except re.error as e:
73
75
  raise ValueError(
74
- "Invalid regex pattern in 'fields' for role '"
76
+ "Invalid regex pattern in 'properties' for role '"
75
77
  + role_name
76
78
  + "' action '"
77
79
  + action
@@ -87,7 +89,7 @@ def validate_permissions(permissions):
87
89
  else:
88
90
  raise ValueError(
89
91
  f"Rule for role '{role_name}' action '{action}' "
90
- f"must be a string or object with 'fields' "
92
+ f"must be a string or object with 'properties' "
91
93
  f"(and optional 'where')."
92
94
  )
93
95
 
@@ -130,22 +132,25 @@ def validate_permissions(permissions):
130
132
  f"'read', 'write', and 'delete'."
131
133
  )
132
134
 
133
- # Detect legacy form: role -> {action: rule}
135
+ # Detect legacy form: role -> {action: rule} or role with 'where'
134
136
  def _is_legacy_form(obj: dict) -> bool:
135
137
  if not obj:
136
138
  return False
137
139
  # If any top-level value is a dict where an action maps to a non-dict
138
140
  # rule (e.g., string/bool/object rule), it's the legacy form.
141
+ # Also detect if there's a 'where' key at the role level.
139
142
  for v in obj.values():
140
143
  if isinstance(v, dict):
144
+ # Check for role-level 'where' clause (hybrid approach)
145
+ if "where" in v:
146
+ return True
141
147
  for k2, v2 in v.items():
142
- if k2 in {"read", "write", "delete"} and not isinstance(
143
- v2, dict
144
- ):
148
+ actions = {"read", "write", "delete"}
149
+ if k2 in actions and not isinstance(v2, dict):
145
150
  return True
146
151
  return False
147
152
 
148
- # Legacy path: {role: {read|write|delete: rule}}
153
+ # Legacy path: {role: {read|write|delete: rule, where?: string}}
149
154
  if _is_legacy_form(permissions):
150
155
  for role_name, actions in permissions.items():
151
156
  if not isinstance(role_name, str):
@@ -156,7 +161,15 @@ def validate_permissions(permissions):
156
161
  f"of actions."
157
162
  )
158
163
  for action, rule in actions.items():
159
- _validate_rule(action, role_name, rule)
164
+ if action == "where":
165
+ # Validate role-level WHERE clause
166
+ if not isinstance(rule, str):
167
+ raise ValueError(
168
+ f"Role-level 'where' clause for role "
169
+ f"'{role_name}' must be a string."
170
+ )
171
+ else:
172
+ _validate_rule(action, role_name, rule)
160
173
  return True
161
174
 
162
175
  # New form: {provider: {action: {role: rule}}}
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "api-foundry"
7
- version = "0.1.20"
7
+ version = "0.1.30"
8
8
  authors = [
9
9
  { name="Daniel Repik", email="danrepik@icloud.com" },
10
10
  ]
File without changes
File without changes