api-foundry 0.8.10__tar.gz → 0.8.12__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
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: api-foundry
3
- Version: 0.8.10
3
+ Version: 0.8.12
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
@@ -25,3 +25,4 @@ Requires-Dist: docker; extra == 'dev'
25
25
  Requires-Dist: psycopg2-binary; extra == 'dev'
26
26
  Requires-Dist: pulumi-automation; extra == 'dev'
27
27
  Requires-Dist: pytest>=7.0; extra == 'dev'
28
+ Requires-Dist: simple-oauth-server; extra == 'dev'
@@ -1,5 +1,5 @@
1
1
  # __init__.py
2
- __version__ = "0.8.10"
2
+ __version__ = "0.8.12"
3
3
 
4
4
  from api_foundry.iac.pulumi.api_foundry import APIFoundry
5
5
  from cloud_foundry import logger
@@ -227,17 +227,24 @@ class APISpecEditor:
227
227
  if len(regex_pattern) == 0:
228
228
  regex_pattern = ".*"
229
229
 
230
- return (
230
+ full_pattern = (
231
231
  rf"^{regex_pattern}$"
232
232
  + rf"|^(?:lt::|le::|eq::|ne::|ge::|gt::)?{regex_pattern}$"
233
233
  + rf"|^between::{regex_pattern},{regex_pattern}$"
234
234
  + rf"|^not-between::{regex_pattern},{regex_pattern},"
235
235
  + rf"|^in::{regex_pattern}(,{regex_pattern})*$"
236
236
  + rf"|^not-in::{regex_pattern}(,{regex_pattern})*$"
237
- + r"|^like::.+$"
238
- + r"|^not-like::.+$"
239
237
  )
240
238
 
239
+ # like/not-like only make sense for string-typed properties -- an
240
+ # integer or number property matching "like::123" is meaningless,
241
+ # and unconditionally allowing it let non-string filters through
242
+ # that the SQL layer can't actually execute as a LIKE comparison.
243
+ if property["type"] == "string":
244
+ full_pattern += r"|^like::.+$" + r"|^not-like::.+$"
245
+
246
+ return full_pattern
247
+
241
248
  def generate_query_parameters(self, schema_object: dict[str, Any]):
242
249
  parameters = []
243
250
  for (
@@ -122,7 +122,7 @@ class APIFoundry(ComponentResource):
122
122
  *,
123
123
  api_spec: Union[str, list[str]],
124
124
  batch_path: Optional[str] = None,
125
- secrets: Optional[str] = None,
125
+ secrets: Optional[Union[str, pulumi.Output[str]]] = None,
126
126
  path_prefix: Optional[str] = None,
127
127
  environment: Optional[dict[str, Union[str, pulumi.Output[str]]]] = None,
128
128
  integrations: Optional[list[dict]] = None,
@@ -158,25 +158,38 @@ class APIFoundry(ComponentResource):
158
158
 
159
159
  # Grant read access to referenced secrets
160
160
  if secrets:
161
- try:
162
- secret_arns = list(json.loads(secrets).values())
163
- policy_statements.append(
164
- {
165
- "Effect": "Allow",
166
- "Actions": ["secretsmanager:GetSecretValue"],
167
- "Resources": secret_arns,
168
- }
169
- )
170
- except (json.JSONDecodeError, AttributeError):
171
- # If secrets is an Output or invalid JSON, we can't parse it yet
172
- # Add a broad policy (will be restricted during deployment)
161
+ if isinstance(secrets, pulumi.Output):
162
+ # Resolve the ARNs at deploy time rather than widening the
163
+ # grant to "*". The statement list is resolved as a whole
164
+ # before it reaches cloud_foundry (see below).
173
165
  policy_statements.append(
174
166
  {
175
167
  "Effect": "Allow",
176
168
  "Actions": ["secretsmanager:GetSecretValue"],
177
- "Resources": "*",
169
+ "Resources": secrets.apply(
170
+ lambda s: list(json.loads(s).values())
171
+ ),
178
172
  }
179
173
  )
174
+ else:
175
+ try:
176
+ secret_arns = list(json.loads(secrets).values())
177
+ policy_statements.append(
178
+ {
179
+ "Effect": "Allow",
180
+ "Actions": ["secretsmanager:GetSecretValue"],
181
+ "Resources": secret_arns,
182
+ }
183
+ )
184
+ except (json.JSONDecodeError, AttributeError):
185
+ # Invalid JSON: fall back to a broad policy
186
+ policy_statements.append(
187
+ {
188
+ "Effect": "Allow",
189
+ "Actions": ["secretsmanager:GetSecretValue"],
190
+ "Resources": "*",
191
+ }
192
+ )
180
193
 
181
194
  requirements.extend(
182
195
  [
@@ -212,6 +225,12 @@ class APIFoundry(ComponentResource):
212
225
  if env_vars.get("JWKS_HOST"):
213
226
  requirements.extend(["PyJWT", "cryptography", "requests"])
214
227
 
228
+ if isinstance(secrets, pulumi.Output):
229
+ # cloud_foundry builds the role policy from plain statements, but
230
+ # accepts the statement list itself as an Output. Resolve the
231
+ # list (including Outputs nested in Resources) as one Output.
232
+ policy_statements = pulumi.Output.from_input(policy_statements)
233
+
215
234
  self.api_function = cloud_foundry.python_function(
216
235
  name=name,
217
236
  environment=env_vars,
@@ -25,7 +25,7 @@ class PostgresSchemaToOpenAPI:
25
25
 
26
26
  def get_columns(self, table_name):
27
27
  query = """
28
- SELECT column_name, data_type, is_nullable, character_maximum_length,
28
+ SELECT column_name, data_type, udt_name, is_nullable, character_maximum_length,
29
29
  (SELECT ccu.table_name
30
30
  FROM information_schema.table_constraints AS tc
31
31
  JOIN information_schema.constraint_column_usage AS ccu
@@ -95,6 +95,35 @@ class PostgresSchemaToOpenAPI:
95
95
  }
96
96
  return type_mapping.get(data_type, {"type": "string"})
97
97
 
98
+ # Element types of Postgres array columns, by udt_name without its
99
+ # leading underscore ("_text" is text[]).
100
+ ARRAY_ELEMENT_TYPES = {
101
+ "text": {"type": "string"},
102
+ "varchar": {"type": "string"},
103
+ "bpchar": {"type": "string"},
104
+ "uuid": {"type": "string", "format": "uuid"},
105
+ "int2": {"type": "integer"},
106
+ "int4": {"type": "integer"},
107
+ "int8": {"type": "integer"},
108
+ "numeric": {"type": "number"},
109
+ "float4": {"type": "number"},
110
+ "float8": {"type": "number"},
111
+ "bool": {"type": "boolean"},
112
+ "date": {"type": "string", "format": "date"},
113
+ "timestamptz": {"type": "string", "format": "date-time"},
114
+ "timestamp": {"type": "string", "format": "date-time"},
115
+ }
116
+
117
+ def map_column(self, column):
118
+ """The OpenAPI property for a column. An array column becomes
119
+ ``type: array`` with an array ``x-af-column-type`` such as text[],
120
+ which the query engine writes as a native array."""
121
+ if column["data_type"] == "ARRAY":
122
+ element = (column.get("udt_name") or "").lstrip("_")
123
+ items = dict(self.ARRAY_ELEMENT_TYPES.get(element, {"type": "string"}))
124
+ return {"type": "array", "items": items, "x-af-column-type": f"{element or 'text'}[]"}
125
+ return self.map_data_type(column["data_type"])
126
+
98
127
  def generate_openapi_schema(self):
99
128
  openapi_schema = {
100
129
  "openapi": "3.0.0",
@@ -113,7 +142,7 @@ class PostgresSchemaToOpenAPI:
113
142
 
114
143
  for column in columns:
115
144
  column_name = column["column_name"]
116
- column_info = self.map_data_type(column["data_type"])
145
+ column_info = self.map_column(column)
117
146
 
118
147
  if column_name in primary_keys:
119
148
  column_info["x-af-primary-key"] = primary_keys[column_name]
@@ -36,6 +36,12 @@ SUPPORTED_TYPES = {
36
36
  }
37
37
 
38
38
 
39
+
40
+ # A Postgres array column type: a type name followed by "[]" ("text[]",
41
+ # "integer[]", "character varying[]"). The query engine interpolates the
42
+ # element type into SQL casts, so nothing else is accepted.
43
+ ARRAY_COLUMN_TYPE = re.compile(r"^\s*[A-Za-z][A-Za-z0-9_ ]*?\s*\[\]\s*$")
44
+
39
45
  class OpenAPIElement:
40
46
  """
41
47
  Base class for OpenAPI elements like schema properties and
@@ -116,6 +122,7 @@ class SchemaObjectProperty(OpenAPIElement):
116
122
  )
117
123
  else:
118
124
  self.column_type = prop.get("type") or "string"
125
+ self._validate_array_column(schema_name, name, prop)
119
126
  self.required = prop.get("required", False)
120
127
  self.min_length = prop.get("minLength", None)
121
128
  self.max_length = prop.get("maxLength", None)
@@ -133,6 +140,34 @@ class SchemaObjectProperty(OpenAPIElement):
133
140
  self.sub_properties: Optional[Dict[str, SchemaObjectProperty]] = None
134
141
  self.items_sub_properties: Optional[Dict[str, SchemaObjectProperty]] = None
135
142
 
143
+ def _validate_array_column(
144
+ self, schema_name: str, name: str, prop: Dict[str, Any]
145
+ ) -> None:
146
+ """A Postgres array column ("text[]", "integer[]", ...) holds a
147
+ list of scalars, so its property must be ``type: array`` with
148
+ scalar items. The element type ends up in SQL casts, so it must be
149
+ a plain type name."""
150
+ column_type = self.column_type
151
+ if not isinstance(column_type, str) or "[" not in column_type:
152
+ return
153
+ where = f"Property: {name} in schema object: {schema_name}"
154
+ if not ARRAY_COLUMN_TYPE.match(column_type):
155
+ raise ApplicationException(
156
+ 500,
157
+ f"{where} has x-af-column-type {column_type!r}; an array column "
158
+ "type is a type name followed by [], such as text[]",
159
+ )
160
+ items = prop.get("items") or {}
161
+ if prop.get("type") != "array" or "$ref" in items or items.get("type") in (
162
+ "object",
163
+ "array",
164
+ ):
165
+ raise ApplicationException(
166
+ 500,
167
+ f"{where} has array x-af-column-type {column_type!r}; it must be "
168
+ "type: array with scalar items",
169
+ )
170
+
136
171
  def _concurrency_control(self, schema_name: str, prop_dict: dict) -> Optional[str]:
137
172
  concurrency_control = prop_dict.get("x-af-concurrency-control", None)
138
173
  if concurrency_control:
@@ -677,8 +712,15 @@ class PathOperation(OpenAPIElement):
677
712
  properties[name] = SchemaObjectProperty(self.entity, name, prop_schema)
678
713
  elif section == "parameters":
679
714
  for param in path_operation.get("parameters", {}) or []:
715
+ # OpenAPI parameter objects nest type/default/etc. under
716
+ # `schema` (unlike requestBody/responses properties, which
717
+ # are already flat) -- merge schema over the parameter
718
+ # object so type/default/minLength/etc. are read from the
719
+ # right place while still keeping parameter-level fields
720
+ # like `required` and `description`.
721
+ merged = {**param, **(param.get("schema") or {})}
680
722
  properties[param["name"]] = SchemaObjectProperty(
681
- self.entity, param["name"], param
723
+ self.entity, param["name"], merged
682
724
  )
683
725
  elif section == "responses":
684
726
  responses = path_operation.get("responses", {})
@@ -35,6 +35,9 @@ dev = [
35
35
  "docker",
36
36
  "api_foundry_query_engine",
37
37
  "pulumi-automation",
38
+ # tests/conftest.py imports this directly; was missing here entirely
39
+ # until CI first ran the test suite and caught it.
40
+ "simple-oauth-server",
38
41
  ]
39
42
 
40
43
  [project.urls]
File without changes
File without changes