github.com/furusax0621/goa-v1@v1.4.3/goagen/gen_schema/json_schema_test.go (about) 1 package genschema_test 2 3 import ( 4 "github.com/goadesign/goa/design" 5 . "github.com/goadesign/goa/design/apidsl" 6 "github.com/goadesign/goa/dslengine" 7 genschema "github.com/goadesign/goa/goagen/gen_schema" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("TypeSchema", func() { 13 var typ design.DataType 14 15 var s *genschema.JSONSchema 16 17 BeforeEach(func() { 18 typ = nil 19 s = nil 20 dslengine.Reset() 21 design.ProjectedMediaTypes = make(design.MediaTypeRoot) 22 }) 23 24 JustBeforeEach(func() { 25 s = genschema.TypeSchema(design.Design, typ) 26 }) 27 28 Context("with a media type", func() { 29 BeforeEach(func() { 30 MediaType("application/foo.bar", func() { 31 Attributes(func() { 32 Attribute("bar", func() { ReadOnly() }) 33 }) 34 View("default", func() { 35 Attribute("bar") 36 }) 37 }) 38 39 Ω(dslengine.Run()).ShouldNot(HaveOccurred()) 40 typ = design.Design.MediaTypes["application/foo.bar"] 41 }) 42 43 It("returns a proper JSON schema type", func() { 44 Ω(s).ShouldNot(BeNil()) 45 Ω(s.Ref).Should(Equal("#/definitions/FooBar")) 46 }) 47 }) 48 49 Context("with a media type with self-referencing attributes", func() { 50 BeforeEach(func() { 51 MediaType("application/vnd.menu+json", func() { 52 Attributes(func() { 53 Attribute("name", design.String, "The name of an application") 54 Attribute("children", CollectionOf("application/vnd.menu+json"), func() { 55 View("nameonly") 56 }) 57 58 }) 59 View("default", func() { 60 Attribute("name") 61 Attribute("children", func() { 62 View("nameonly") 63 }) 64 }) 65 View("nameonly", func() { 66 Attribute("name") 67 }) 68 }) 69 70 Ω(func() { dslengine.Run() }).ShouldNot(Panic()) 71 Ω(dslengine.Errors).ShouldNot(HaveOccurred()) 72 typ = design.Design.MediaTypes["application/vnd.menu"] 73 }) 74 75 It("returns a proper JSON schema type", func() { 76 Ω(s).ShouldNot(BeNil()) 77 Ω(s.Ref).Should(Equal("#/definitions/Menu")) 78 }) 79 80 }) 81 })