From c7bd41cdaef023747c5fb06588072002d98b6f6c Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Thu, 21 Aug 2025 18:41:19 +0100 Subject: [PATCH] encoding/openapi: attempt round-trip of generated OpenAPI schema Reading through the encoding/openapi code, it was clear that there were some mistakes that weren't being caught by the tests, notably a misspelling of `allOf` as `allOff`. Here we add a sanity check to the txtar tests that we can actually translate the resulting schema back to CUE again. We can then see that round-tripping does indeed fail in some cases. We'll fix those cases in subsequent CLs. While we're about it, use the non-deprecated `Generate` function as the primary entry point for testing. Signed-off-by: Roger Peppe Change-Id: Ied10e2c744960d757071145ef6cf1b29762d4346 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1220859 Unity-Result: CUE porcuepine TryBot-Result: CUEcueckoo Reviewed-by: Matthew Sackman --- encoding/openapi/openapi_test.go | 35 +++++++++++++++--------- encoding/openapi/testdata/array.txtar | 1 + encoding/openapi/testdata/builtins.txtar | 1 + encoding/openapi/testdata/nums.txtar | 1 + 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/encoding/openapi/openapi_test.go b/encoding/openapi/openapi_test.go index f6a82dfa1..9c80cc178 100644 --- a/encoding/openapi/openapi_test.go +++ b/encoding/openapi/openapi_test.go @@ -20,6 +20,8 @@ import ( "strings" "testing" + "github.com/go-quicktest/qt" + "cuelang.org/go/cue" "cuelang.org/go/cue/cuecontext" "cuelang.org/go/cue/errors" @@ -104,7 +106,7 @@ func TestGenerateOpenAPI(t *testing.T) { } expectedErr, shouldErr := t.Value("ExpectError") - b, err := openapi.Gen(v, &config) + f, err := openapi.Generate(v, &config) if err != nil { details := errors.Details(err, nil) if !shouldErr || !strings.Contains(details, expectedErr) { @@ -112,27 +114,34 @@ func TestGenerateOpenAPI(t *testing.T) { } return } - if shouldErr { t.Fatal("unexpected success") } else { - _, err := openapi.Generate(v, &config) + _, err := openapi.Gen(v, &config) if err != nil { t.Fatal(err) } } - + gen := ctx.BuildFile(f) + qt.Assert(t, qt.IsNil(gen.Err())) var out bytes.Buffer - err = json.Indent(&out, b, "", " ") - if err != nil { - t.Fatal(err) - } - - w := t.Writer("out.json") - _, err = w.Write(out.Bytes()) - if err != nil { - t.Fatal(err) + enc := json.NewEncoder(&out) + enc.SetEscapeHTML(false) + enc.SetIndent("", " ") + err = enc.Encode(gen) + qt.Assert(t, qt.IsNil(err)) + _, err = t.Writer("out.json").Write(out.Bytes()) + qt.Assert(t, qt.IsNil(err)) + + // Check that we can extract the resulting schema without error. + _, err = openapi.Extract(gen, &config) + if expectedErr, shouldErr := t.Value("ExpectExtractError"); shouldErr { + qt.Assert(t, qt.ErrorMatches(err, expectedErr)) + return } + // TODO check that the resulting schema actually validates some + // data values as expected. + qt.Assert(t, qt.IsNil(err)) }) } diff --git a/encoding/openapi/testdata/array.txtar b/encoding/openapi/testdata/array.txtar index a3bb3b8b4..3911a61e6 100644 --- a/encoding/openapi/testdata/array.txtar +++ b/encoding/openapi/testdata/array.txtar @@ -1,3 +1,4 @@ +#ExpectExtractError: "items" must be present when the "type" is "array" in OpenAPI 3.0.* -- in.cue -- import "list" diff --git a/encoding/openapi/testdata/builtins.txtar b/encoding/openapi/testdata/builtins.txtar index 3132b64ee..53a8e9d80 100644 --- a/encoding/openapi/testdata/builtins.txtar +++ b/encoding/openapi/testdata/builtins.txtar @@ -1,3 +1,4 @@ +#ExpectExtractError: "items" must be present when the "type" is "array" in OpenAPI 3.0.* -- in.cue -- import ( "time" diff --git a/encoding/openapi/testdata/nums.txtar b/encoding/openapi/testdata/nums.txtar index a5d86656b..a4ea064a3 100644 --- a/encoding/openapi/testdata/nums.txtar +++ b/encoding/openapi/testdata/nums.txtar @@ -1,3 +1,4 @@ +#ExpectExtractError: unknown keyword "allOff".* -- in.cue -- import "math" -- 2.51.2