encoding/openapi: support Definitions

Fixes #131

Change-Id: Ic7581a22dddb0c6b01707c78ea9b051abfa0e0e2
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3584
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/openapi/build.go b/encoding/openapi/build.go
index 5fee0a5..8dc1db7 100644
--- a/encoding/openapi/build.go
+++ b/encoding/openapi/build.go
@@ -101,7 +101,7 @@
 
 	// Although paths is empty for now, it makes it valid OpenAPI spec.
 
-	i, err := inst.Value().Fields()
+	i, err := inst.Value().Fields(cue.Definitions(true))
 	if err != nil {
 		return nil, err
 	}
@@ -281,7 +281,11 @@
 	switch op, a := v.Expr(); op {
 	case cue.SelectorOp:
 		field, _ := a[1].String()
-		v = b.resolve(a[0]).Lookup(field)
+		f, _ := b.resolve(a[0]).LookupField(field)
+		v = cue.Value{}
+		if !f.IsOptional {
+			v = f.Value
+		}
 	}
 	return v
 }
@@ -615,7 +619,7 @@
 	}
 
 	required := []string{}
-	for i, _ := v.Fields(cue.Optional(false), cue.Hidden(false)); i.Next(); {
+	for i, _ := v.Fields(); i.Next(); {
 		required = append(required, i.Label())
 	}
 	if len(required) > 0 {
@@ -631,7 +635,7 @@
 		properties = &OrderedMap{}
 	}
 
-	for i, _ := v.Fields(cue.Optional(true), cue.Hidden(false)); i.Next(); {
+	for i, _ := v.Fields(cue.Optional(true)); i.Next(); {
 		label := i.Label()
 		var core *builder
 		if b.core != nil {