diff --git a/docs/content/rest-api.md b/docs/content/rest-api.md index c4552ce7e8..04228026d3 100644 --- a/docs/content/rest-api.md +++ b/docs/content/rest-api.md @@ -1281,10 +1281,11 @@ Evaluation in OPA, see [this post on blog.openpolicyagent.org](https://blog.open Compile API requests contain the following fields: -| Field | Type | Requried | Description | +| Field | Type | Required | Description | | --- | --- | --- | --- | | `query` | `string` | Yes | The query to partially evaluate and compile. | | `input` | `any` | No | The input document to use during partial evaluation (default: undefined). | +| `options` | `object[string, any]` | No | Additional options to use during partial evaluation. Only `disableInlining` option is supported. (default: undefined). | | `unknowns` | `array[string]` | No | The terms to treat as unknown during partial evaluation (default: `["input"]`]). | #### Query Parameters @@ -1325,6 +1326,9 @@ Content-Type: application/json "clearance_level": 4 } }, + "options": { + "disableInlining": [] + }, "unknowns": [ "data.reports" ] diff --git a/server/server.go b/server/server.go index 6a8b68dbb4..46ba8976a3 100644 --- a/server/server.go +++ b/server/server.go @@ -1242,6 +1242,7 @@ func (s *Server) v1CompilePost(w http.ResponseWriter, r *http.Request) { rego.ParsedQuery(request.Query), rego.ParsedInput(request.Input), rego.ParsedUnknowns(request.Unknowns), + rego.DisableInlining(request.Options.DisableInlining), rego.QueryTracer(buf), rego.Instrument(includeInstrumentation), rego.Metrics(m), @@ -2670,6 +2671,11 @@ type compileRequest struct { Query ast.Body Input ast.Value Unknowns []*ast.Term + Options compileRequestOptions +} + +type compileRequestOptions struct { + DisableInlining []string } func readInputCompilePostV1(r io.ReadCloser) (*compileRequest, *types.ErrorV1) { @@ -2716,6 +2722,9 @@ func readInputCompilePostV1(r io.ReadCloser) (*compileRequest, *types.ErrorV1) { Query: query, Input: input, Unknowns: unknowns, + Options: compileRequestOptions{ + DisableInlining: request.Options.DisableInlining, + }, } return result, nil diff --git a/server/server_test.go b/server/server_test.go index feab720af6..3bbd4361c7 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -845,6 +845,10 @@ func TestCompileV1(t *testing.T) { default r = true r { input.x = 1 } + + custom_func(x) { data.a[i] == x } + + s { custom_func(input.x) } ` expQuery := func(s string) string { @@ -915,6 +919,34 @@ func TestCompileV1(t *testing.T) { `)}, }, }, + { + note: "function without disableInlining", + trs: []tr{ + {http.MethodPut, "/policies/test", mod, 200, ""}, + {http.MethodPost, "/compile", `{ + "unknowns": ["data.a"], + "query": "data.test.s = true", + "input": { "x": 1 } + }`, 200, expQuery("data.a[i2] = 1")}, + }, + }, + { + note: "function with disableInlining", + trs: []tr{ + {http.MethodPut, "/policies/test", mod, 200, ""}, + {http.MethodPost, "/compile", `{ + "unknowns": ["data.a"], + "query": "data.test.s = true", + "options": { "disableInlining": ["data.test"] }, + "input": { "x": 1 } + }`, 200, expQueryAndSupport( + `data.partial.test.s = true`, + `package partial.test + s { data.partial.test.custom_func(1) } + custom_func(__local0__2) { data.a[i2] = __local0__2 } + `)}, + }, + }, { note: "empty unknowns", trs: []tr{ diff --git a/server/types/types.go b/server/types/types.go index c5d3d74ea3..acc2da8bee 100644 --- a/server/types/types.go +++ b/server/types/types.go @@ -352,6 +352,9 @@ type CompileRequestV1 struct { Input *interface{} `json:"input"` Query string `json:"query"` Unknowns *[]string `json:"unknowns"` + Options struct { + DisableInlining []string `json:"disableInlining,omitempty"` + } `json:"options,omitempty"` } // CompileResponseV1 models the response message for Compile API operations.