This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/main by this push:
     new 492018902c Open-API: Refactor updates with discriminator (#9240)
492018902c is described below

commit 492018902cd044adb8bdc312f01d70a4a63cdcc0
Author: Fokko Driesprong <[email protected]>
AuthorDate: Mon Dec 11 09:13:52 2023 +0100

    Open-API: Refactor updates with discriminator (#9240)
    
    * Open-API: Refactor updates with discriminator
    
    This generates nicer code
    
    * Add missing
---
 open-api/rest-catalog-open-api.py   |  37 ++---
 open-api/rest-catalog-open-api.yaml | 321 +++++++++++++++++++++---------------
 2 files changed, 205 insertions(+), 153 deletions(-)

diff --git a/open-api/rest-catalog-open-api.py 
b/open-api/rest-catalog-open-api.py
index 6cd60fe9ab..cc70d6d4cd 100644
--- a/open-api/rest-catalog-open-api.py
+++ b/open-api/rest-catalog-open-api.py
@@ -239,25 +239,7 @@ class ViewVersion(BaseModel):
 
 
 class BaseUpdate(BaseModel):
-    action: Literal[
-        'assign-uuid',
-        'upgrade-format-version',
-        'add-schema',
-        'set-current-schema',
-        'add-spec',
-        'set-default-spec',
-        'add-sort-order',
-        'set-default-sort-order',
-        'add-snapshot',
-        'set-snapshot-ref',
-        'remove-snapshots',
-        'remove-snapshot-ref',
-        'set-location',
-        'set-properties',
-        'remove-properties',
-        'add-view-version',
-        'set-current-view-version',
-    ]
+    action: str
 
 
 class AssignUUIDUpdate(BaseUpdate):
@@ -265,14 +247,17 @@ class AssignUUIDUpdate(BaseUpdate):
     Assigning a UUID to a table/view should only be done when creating the 
table/view. It is not safe to re-assign the UUID if a table/view already has a 
UUID assigned
     """
 
+    action: Literal['assign-uuid']
     uuid: str
 
 
 class UpgradeFormatVersionUpdate(BaseUpdate):
+    action: Literal['upgrade-format-version']
     format_version: int = Field(..., alias='format-version')
 
 
 class SetCurrentSchemaUpdate(BaseUpdate):
+    action: Literal['set-current-schema']
     schema_id: int = Field(
         ...,
         alias='schema-id',
@@ -281,10 +266,12 @@ class SetCurrentSchemaUpdate(BaseUpdate):
 
 
 class AddPartitionSpecUpdate(BaseUpdate):
+    action: Literal['add-spec']
     spec: PartitionSpec
 
 
 class SetDefaultSpecUpdate(BaseUpdate):
+    action: Literal['set-default-spec']
     spec_id: int = Field(
         ...,
         alias='spec-id',
@@ -293,10 +280,12 @@ class SetDefaultSpecUpdate(BaseUpdate):
 
 
 class AddSortOrderUpdate(BaseUpdate):
+    action: Literal['add-sort-order']
     sort_order: SortOrder = Field(..., alias='sort-order')
 
 
 class SetDefaultSortOrderUpdate(BaseUpdate):
+    action: Literal['set-default-sort-order']
     sort_order_id: int = Field(
         ...,
         alias='sort-order-id',
@@ -305,38 +294,47 @@ class SetDefaultSortOrderUpdate(BaseUpdate):
 
 
 class AddSnapshotUpdate(BaseUpdate):
+    action: Literal['add-snapshot']
     snapshot: Snapshot
 
 
 class SetSnapshotRefUpdate(BaseUpdate, SnapshotReference):
+    action: Literal['set-snapshot-ref']
     ref_name: str = Field(..., alias='ref-name')
 
 
 class RemoveSnapshotsUpdate(BaseUpdate):
+    action: Literal['remove-snapshots']
     snapshot_ids: List[int] = Field(..., alias='snapshot-ids')
 
 
 class RemoveSnapshotRefUpdate(BaseUpdate):
+    action: Literal['remove-snapshot-ref']
     ref_name: str = Field(..., alias='ref-name')
 
 
 class SetLocationUpdate(BaseUpdate):
+    action: Literal['set-location']
     location: str
 
 
 class SetPropertiesUpdate(BaseUpdate):
+    action: Literal['set-properties']
     updates: Dict[str, str]
 
 
 class RemovePropertiesUpdate(BaseUpdate):
+    action: Literal['remove-properties']
     removals: List[str]
 
 
 class AddViewVersionUpdate(BaseUpdate):
+    action: Literal['add-view-version']
     view_version: ViewVersion = Field(..., alias='view-version')
 
 
 class SetCurrentViewVersionUpdate(BaseUpdate):
+    action: Literal['set-current-view-version']
     view_version_id: int = Field(
         ...,
         alias='view-version-id',
@@ -734,6 +732,7 @@ class ViewMetadata(BaseModel):
 
 
 class AddSchemaUpdate(BaseUpdate):
+    action: Literal['add-schema']
     schema_: Schema = Field(..., alias='schema')
     last_column_id: Optional[int] = Field(
         None,
diff --git a/open-api/rest-catalog-open-api.yaml 
b/open-api/rest-catalog-open-api.yaml
index a9d30ed02c..0b008ace60 100644
--- a/open-api/rest-catalog-open-api.yaml
+++ b/open-api/rest-catalog-open-api.yaml
@@ -2130,216 +2130,269 @@ components:
             type: string
 
     BaseUpdate:
+      discriminator:
+        propertyName: action
+        mapping:
+          assign-uuid: '#/components/schemas/AssignUUIDUpdate'
+          upgrade-format-version: 
'#/components/schemas/UpgradeFormatVersionUpdate'
+          add-schema: '#/components/schemas/AddSchemaUpdate'
+          set-current-schema: '#/components/schemas/SetCurrentSchemaUpdate'
+          add-spec: '#/components/schemas/AddPartitionSpecUpdate'
+          set-default-spec: '#/components/schemas/SetDefaultSpecUpdate'
+          add-sort-order: '#/components/schemas/AddSortOrderUpdate'
+          set-default-sort-order: 
'#/components/schemas/SetDefaultSortOrderUpdate'
+          add-snapshot: '#/components/schemas/AddSnapshotUpdate'
+          set-snapshot-ref: '#/components/schemas/SetSnapshotRefUpdate'
+          remove-snapshots: '#/components/schemas/RemoveSnapshotsUpdate'
+          remove-snapshot-ref: '#/components/schemas/RemoveSnapshotRefUpdate'
+          set-location: '#/components/schemas/SetLocationUpdate'
+          set-properties: '#/components/schemas/SetPropertiesUpdate'
+          remove-properties: '#/components/schemas/RemovePropertiesUpdate'
+          add-view-version: '#/components/schemas/AddViewVersionUpdate'
+          set-current-view-version: 
'#/components/schemas/SetCurrentViewVersionUpdate'
       type: object
       required:
         - action
       properties:
         action:
           type: string
-          enum:
-            - assign-uuid
-            - upgrade-format-version
-            - add-schema
-            - set-current-schema
-            - add-spec
-            - set-default-spec
-            - add-sort-order
-            - set-default-sort-order
-            - add-snapshot
-            - set-snapshot-ref
-            - remove-snapshots
-            - remove-snapshot-ref
-            - set-location
-            - set-properties
-            - remove-properties
-            - add-view-version
-            - set-current-view-version
 
     AssignUUIDUpdate:
       description: Assigning a UUID to a table/view should only be done when 
creating the table/view. It is not safe to re-assign the UUID if a table/view 
already has a UUID assigned
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - uuid
-          properties:
-            uuid:
-              type: string
+      required:
+        - action
+        - uuid
+      properties:
+        action:
+          type: string
+          enum: ["assign-uuid"]
+        uuid:
+          type: string
 
     UpgradeFormatVersionUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - format-version
-          properties:
-            format-version:
-              type: integer
+      required:
+        - action
+        - format-version
+      properties:
+        action:
+          type: string
+          enum: ["upgrade-format-version"]
+        format-version:
+          type: integer
 
     AddSchemaUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - schema
-          properties:
-            schema:
-              $ref: '#/components/schemas/Schema'
-            last-column-id:
-              type: integer
-              description: The highest assigned column ID for the table. This 
is used to ensure columns are always assigned an unused ID when evolving 
schemas. When omitted, it will be computed on the server side.
+      required:
+        - action
+        - schema
+      properties:
+        action:
+          type: string
+          enum: ["add-schema"]
+        schema:
+          $ref: '#/components/schemas/Schema'
+        last-column-id:
+          type: integer
+          description: The highest assigned column ID for the table. This is 
used to ensure columns are always assigned an unused ID when evolving schemas. 
When omitted, it will be computed on the server side.
 
     SetCurrentSchemaUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - schema-id
-          properties:
-            schema-id:
-              type: integer
-              description: Schema ID to set as current, or -1 to set last 
added schema
+      required:
+        - action
+        - schema-id
+      properties:
+        action:
+          type: string
+          enum: ["set-current-schema"]
+        schema-id:
+          type: integer
+          description: Schema ID to set as current, or -1 to set last added 
schema
 
     AddPartitionSpecUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - spec
-          properties:
-            spec:
-              $ref: '#/components/schemas/PartitionSpec'
+      required:
+        - action
+        - spec
+      properties:
+        action:
+          type: string
+          enum: ["add-spec"]
+        spec:
+          $ref: '#/components/schemas/PartitionSpec'
 
     SetDefaultSpecUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - spec-id
-          properties:
-            spec-id:
-              type: integer
-              description: Partition spec ID to set as the default, or -1 to 
set last added spec
+      required:
+        - action
+        - spec-id
+      properties:
+        action:
+          type: string
+          enum: [ "set-default-spec" ]
+        spec-id:
+          type: integer
+          description: Partition spec ID to set as the default, or -1 to set 
last added spec
 
     AddSortOrderUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - sort-order
-          properties:
-            sort-order:
-              $ref: '#/components/schemas/SortOrder'
+      required:
+        - action
+        - sort-order
+      properties:
+        action:
+          type: string
+          enum: [ "add-sort-order" ]
+        sort-order:
+          $ref: '#/components/schemas/SortOrder'
 
     SetDefaultSortOrderUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - sort-order-id
-          properties:
-            sort-order-id:
-              type: integer
-              description: Sort order ID to set as the default, or -1 to set 
last added sort order
+      required:
+        - action
+        - sort-order-id
+      properties:
+        action:
+          type: string
+          enum: [ "set-default-sort-order" ]
+        sort-order-id:
+          type: integer
+          description: Sort order ID to set as the default, or -1 to set last 
added sort order
 
     AddSnapshotUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - snapshot
-          properties:
-            snapshot:
-              $ref: '#/components/schemas/Snapshot'
+      required:
+        - action
+        - snapshot
+      properties:
+        action:
+          type: string
+          enum: [ "add-snapshot" ]
+        snapshot:
+          $ref: '#/components/schemas/Snapshot'
 
     SetSnapshotRefUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
         - $ref: '#/components/schemas/SnapshotReference'
-        - type: object
-          required:
-            - ref-name
-          properties:
-            ref-name:
-              type: string
+      required:
+        - action
+        - ref-name
+      properties:
+        action:
+          type: string
+          enum: [ "set-snapshot-ref" ]
+        ref-name:
+          type: string
 
     RemoveSnapshotsUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - snapshot-ids
-          properties:
-            snapshot-ids:
-              type: array
-              items:
-                type: integer
-                format: int64
+      required:
+        - action
+        - snapshot-ids
+      properties:
+        action:
+          type: string
+          enum: [ "remove-snapshots" ]
+        snapshot-ids:
+          type: array
+          items:
+            type: integer
+            format: int64
 
     RemoveSnapshotRefUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - ref-name
-          properties:
-            ref-name:
-              type: string
+      required:
+        - action
+        - ref-name
+      properties:
+        action:
+          type: string
+          enum: [ "remove-snapshot-ref" ]
+        ref-name:
+          type: string
 
     SetLocationUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - location
-          properties:
-            location:
-              type: string
+      required:
+        - action
+        - location
+      properties:
+        action:
+          type: string
+          enum: [ "set-location" ]
+        location:
+          type: string
 
     SetPropertiesUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - updates
-          properties:
-            updates:
-              type: object
-              additionalProperties:
-                type: string
+      required:
+        - action
+        - updates
+      properties:
+        action:
+          type: string
+          enum: [ "set-properties" ]
+        updates:
+          type: object
+          additionalProperties:
+            type: string
 
     RemovePropertiesUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - removals
-          properties:
-            removals:
-              type: array
-              items:
-                type: string
+      required:
+        - action
+        - removals
+      properties:
+        action:
+          type: string
+          enum: [ "remove-properties" ]
+        removals:
+          type: array
+          items:
+            type: string
 
     AddViewVersionUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - view-version
-          properties:
-            view-version:
-              $ref: '#/components/schemas/ViewVersion'
+      required:
+        - action
+        - view-version
+      properties:
+        action:
+          type: string
+          enum: [ "add-view-version" ]
+        view-version:
+          $ref: '#/components/schemas/ViewVersion'
 
     SetCurrentViewVersionUpdate:
       allOf:
         - $ref: '#/components/schemas/BaseUpdate'
-        - type: object
-          required:
-            - view-version-id
-          properties:
-            view-version-id:
-              type: integer
-              description: The view version id to set as current, or -1 to set 
last added view version id
+      required:
+        - action
+        - view-version-id
+      properties:
+        action:
+          type: string
+          enum: [ "set-current-view-version" ]
+        view-version-id:
+          type: integer
+          description: The view version id to set as current, or -1 to set 
last added view version id
 
     TableUpdate:
       anyOf:

Reply via email to