Azure Static WebApp database connection preview issue

111 views Asked by At

I am using azure static webapp for an angular application using the related staticwebapp.database.config.json:

{
    "$schema": "https://github.com/Azure/data-api-builder/releases/latest/download/dab.draft.schema.json",
    "data-source": {
      "database-type": "mssql",
      "options": {
        "set-session-context": false 
      },
      "connection-string": "@env('DATABASE_CONNECTION_STRING')"
    },
    "runtime": {
      "rest": {
        "enabled": true,
        "path": "/rest"
      },
      "graphql": {
        "allow-introspection": true,
        "enabled": true,
        "path": "/graphql"
      },
      "host": {
        "mode": "production",
        "authentication": {
          "provider": "StaticWebApps"
        }
      }
    },
    "entities": {
      "setup": {
        "source": "dbo.Setup",
        "rest": {
          "path": "/setup",
          "methods": ["get", "post", "put", "delete"],
          "enabled": true
        },
        "permissions": [
          {
            "actions": ["create", "read", "update", "delete"],
            "role": "anonymous"
          }
        ]
      },
      "organizationDetail": {
        "source": {
          "object": "dbo.OrganizationDetail",
          "type": "view"
        },
        "rest": {
          "path": "/organization-detail",
          "methods": ["get"],
          "enabled": true
        },
        "permissions": [
          {
            "actions": ["read"],
            "role": "anonymous"
          }
        ]
      }
    }
  }

I want to try the DataBase connection preview features, I perform the first GET to the following endpoint which target a sql view:

enter image description here

https://[target-static-app]/data-api/rest/organization-detail

...and it works like a charm:

enter image description here

but when I perform a PUT request to the setup endpoint:

enter image description here

url: https://[target-static-app]/data-api/rest/setup/1

payload: {Id: 1, Title: "Test2", PrimaryColor: "#33cc66", SecondaryColor: "#ccff33", OrganizationId: 1}

I can obtain only the following:

"error": {
        "code": "BadRequest",
        "message": "Invalid request body. Either insufficient or extra fields supplied.",
        "status": 400
    }

Following the relevant openapi generated:

{
    "openapi": "3.0.1",
    "info": {
        "title": "Data API builder - REST Endpoint",
        "version": "PREVIEW"
    },
    "servers": [
        {
            "url": "/rest"
        }
    ],
    "paths": {
        "/setup/Id/{Id}": {
            "get": {
                "tags": [
                    "setup"
                ],
                "description": "Returns an entity.",
                "responses": {
                    "400": {
                        "description": "BadRequest"
                    },
                    "401": {
                        "description": "Unauthorized"
                    },
                    "403": {
                        "description": "Forbidden"
                    },
                    "404": {
                        "description": "NotFound"
                    },
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "properties": {
                                        "value": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/setup"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "put": {
                "tags": [
                    "setup"
                ],
                "description": "Replace or create entity.",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/setup_NoPK"
                            }
                        }
                    },
                    "required": true
                },
                "responses": {
                    "400": {
                        "description": "BadRequest"
                    },
                    "401": {
                        "description": "Unauthorized"
                    },
                    "403": {
                        "description": "Forbidden"
                    },
                    "404": {
                        "description": "NotFound"
                    },
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "properties": {
                                        "value": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/setup"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "201": {
                        "description": "Created",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "properties": {
                                        "value": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/setup"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "patch": {
                "tags": [
                    "setup"
                ],
                "description": "Update or create entity.",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/setup_NoPK"
                            }
                        }
                    },
                    "required": true
                },
                "responses": {
                    "400": {
                        "description": "BadRequest"
                    },
                    "401": {
                        "description": "Unauthorized"
                    },
                    "403": {
                        "description": "Forbidden"
                    },
                    "404": {
                        "description": "NotFound"
                    },
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "properties": {
                                        "value": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/setup"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "201": {
                        "description": "Created",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "properties": {
                                        "value": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/setup"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "delete": {
                "tags": [
                    "setup"
                ],
                "description": "Delete entity.",
                "responses": {
                    "400": {
                        "description": "BadRequest"
                    },
                    "401": {
                        "description": "Unauthorized"
                    },
                    "403": {
                        "description": "Forbidden"
                    },
                    "404": {
                        "description": "NotFound"
                    },
                    "204": {
                        "description": "NoContent"
                    }
                }
            },
            "parameters": [
                {
                    "name": "Id",
                    "in": "path",
                    "required": true,
                    "schema": {
                        "type": "number"
                    }
                }
            ]
        },
        "/setup": {
            "get": {
                "tags": [
                    "setup"
                ],
                "description": "Returns entities.",
                "responses": {
                    "400": {
                        "description": "BadRequest"
                    },
                    "401": {
                        "description": "Unauthorized"
                    },
                    "403": {
                        "description": "Forbidden"
                    },
                    "404": {
                        "description": "NotFound"
                    },
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "properties": {
                                        "value": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/setup"
                                            }
                                        },
                                        "nextLink": {
                                            "type": "string"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "post": {
                "tags": [
                    "setup"
                ],
                "description": "Create entity.",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/setup_NoAutoPK"
                            }
                        }
                    },
                    "required": true
                },
                "responses": {
                    "400": {
                        "description": "BadRequest"
                    },
                    "401": {
                        "description": "Unauthorized"
                    },
                    "403": {
                        "description": "Forbidden"
                    },
                    "404": {
                        "description": "NotFound"
                    },
                    "201": {
                        "description": "Created",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "properties": {
                                        "value": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/setup"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "409": {
                        "description": "Conflict"
                    }
                }
            }
        
    },
    "components": {
        "schemas": {
            "setup": {
                "type": "object",
                "properties": {
                    "Id": {
                        "type": "number",
                        "format": ""
                    },
                    "Title": {
                        "type": "string",
                        "format": ""
                    },
                    "PrimaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "SecondaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "OrganizationId": {
                        "type": "number",
                        "format": ""
                    }
                }
            },
            "setup_NoAutoPK": {
                "type": "object",
                "properties": {
                    "Title": {
                        "type": "string",
                        "format": ""
                    },
                    "PrimaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "SecondaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "OrganizationId": {
                        "type": "number",
                        "format": ""
                    }
                }
            },
            "setup_NoPK": {
                "type": "object",
                "properties": {
                    "Title": {
                        "type": "string",
                        "format": ""
                    },
                    "PrimaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "SecondaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "OrganizationId": {
                        "type": "number",
                        "format": ""
                    }
                }
            },
            "organizationDetail": {
                "type": "object",
                "properties": {
                    "Id": {
                        "type": "number",
                        "format": ""
                    },
                    "Name": {
                        "type": "string",
                        "format": ""
                    },
                    "Title": {
                        "type": "string",
                        "format": ""
                    },
                    "PrimaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "SecondaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "SetupId": {
                        "type": "number",
                        "format": ""
                    },
                    "OrganizationId": {
                        "type": "number",
                        "format": ""
                    }
                }
            },
            "organizationDetail_NoAutoPK": {
                "type": "object",
                "properties": {
                    "Id": {
                        "type": "number",
                        "format": ""
                    },
                    "Name": {
                        "type": "string",
                        "format": ""
                    },
                    "Title": {
                        "type": "string",
                        "format": ""
                    },
                    "PrimaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "SecondaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "SetupId": {
                        "type": "number",
                        "format": ""
                    },
                    "OrganizationId": {
                        "type": "number",
                        "format": ""
                    }
                }
            },
            "organizationDetail_NoPK": {
                "type": "object",
                "properties": {
                    "Name": {
                        "type": "string",
                        "format": ""
                    },
                    "Title": {
                        "type": "string",
                        "format": ""
                    },
                    "PrimaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "SecondaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "OrganizationId": {
                        "type": "number",
                        "format": ""
                    }
                }
            }
        }
    }
}

No way to make it work, what I am missing?

1

There are 1 answers

1
akseli On BEST ANSWER

I believe that your issue is that your PUT command is expecting the setup_NoPK schema, which, according to the definition in your file, doesn't include the PrimaryKey field, which in your case is Id.

"put": {
                "tags": [
                    "setup"
                ],
                "description": "Replace or create entity.",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/setup_NoPK"
                            }
                        }
                    },
                    "required": true
                },

Thus, when you send your PUT request with the Id field filled out, validation is throwing is back as an overpost.

If you look at the documentation here you can see that the Id value for the entity is passed as a parameter in the URL itself for PUT and PATCH calls.

https://[target-static-app]/data-api/rest/setup/1

This means that in your actual payload, your content should not include the Id value, like this:

payload: {Title: "Test2", PrimaryColor: "#33cc66", SecondaryColor: "#ccff33", OrganizationId: 1}