api-foundry 0.8.11__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
1
  Metadata-Version: 2.5
2
2
  Name: api-foundry
3
- Version: 0.8.11
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
@@ -1,5 +1,5 @@
1
1
  # __init__.py
2
- __version__ = "0.8.11"
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
@@ -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:
File without changes
File without changes