Skip to content

Commit 15adb6c

Browse files
committed
encoding/openapi: fix crash in reference
For certain values, inst could be nil, even though there were references. The code in OpenAPI was a workaround around references, though, and seems no longer necessary. Fixes #1270 Signed-off-by: Marcel van Lohuizen <mpvl@golang.org> Change-Id: Ie2a9d0a5ddc669f77e21fd5d2128341613c6510a Signed-off-by: Marcel van Lohuizen <mpvl@golang.org> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/527871 Unity-Result: CUEcueckoo <cueckoo@cuelang.org> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Reviewed-by: Paul Jolly <paul@myitcv.io>
1 parent 4e76bb4 commit 15adb6c

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
cue def test.cue test.json -o openapi:- -e '{#Output: #Test}'
2+
3+
cmp stdout stdout.golden
4+
5+
-- test.cue --
6+
#Int: int
7+
#Test: test: #Int
8+
9+
-- test.json --
10+
{ }
11+
12+
-- stdout.golden --
13+
{
14+
"openapi": "3.0.0",
15+
"info": {
16+
"title": "Generated by cue.",
17+
"version": "no version"
18+
},
19+
"paths": {},
20+
"components": {
21+
"schemas": {
22+
"Int": {
23+
"type": "integer"
24+
},
25+
"Output": {
26+
"$ref": "#/components/schemas/Test"
27+
},
28+
"Test": {
29+
"type": "object",
30+
"required": [
31+
"test"
32+
],
33+
"properties": {
34+
"test": {
35+
"$ref": "#/components/schemas/Int"
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}

encoding/openapi/types.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package openapi
1616

1717
import (
1818
"fmt"
19-
"strings"
2019

2120
"github.com/cockroachdb/apd/v2"
2221

@@ -58,21 +57,17 @@ func extractFormat(v cue.Value) string {
5857
default:
5958
return ""
6059
}
61-
var expr, arg string
62-
op, a := v.Expr()
63-
if op == cue.CallOp {
60+
var arg string
61+
62+
if op, a := v.Expr(); op == cue.CallOp {
6463
v = a[0]
6564
if len(a) == 2 {
6665
arg = fmt.Sprintf(" (%v)", a[1].Eval())
6766
}
6867
}
69-
if inst, ref := v.Reference(); len(ref) > 0 {
70-
expr = inst.ImportPath + "." + strings.Join(ref, ".")
71-
expr += arg
72-
} else {
73-
expr = fmt.Sprint(v.Eval())
74-
expr += arg
75-
}
68+
69+
expr := fmt.Sprint(v.Eval(), arg)
70+
7671
if s, ok := cueToOpenAPI[expr]; ok {
7772
return s
7873
}

0 commit comments

Comments
 (0)