func newMetaParser(swspec *spec.Swagger) *sectionedParser { sp := new(sectionedParser) if swspec.Info == nil { swspec.Info = new(spec.Info) } info := swspec.Info sp.setTitle = func(lines []string) { tosave := joinDropLast(lines) if len(tosave) > 0 { tosave = rxStripTitleComments.ReplaceAllString(tosave, "") } info.Title = tosave } sp.setDescription = func(lines []string) { info.Description = joinDropLast(lines) } sp.taggers = []tagParser{ newMultiLineTagParser("TOS", newMultilineDropEmptyParser(rxTOS, metaTOSSetter(info))), newMultiLineTagParser("Consumes", newMultilineDropEmptyParser(rxConsumes, metaConsumesSetter(swspec))), newMultiLineTagParser("Produces", newMultilineDropEmptyParser(rxProduces, metaProducesSetter(swspec))), newSingleLineTagParser("Schemes", newSetSchemes(metaSchemeSetter(swspec))), newSingleLineTagParser("SecurityDefinitions", newSetSecurityDefinitions(rxSecurity, metaSecuritySetter(swspec))), newSingleLineTagParser("Version", &setMetaSingle{swspec, rxVersion, setInfoVersion}), newSingleLineTagParser("Host", &setMetaSingle{swspec, rxHost, setSwaggerHost}), newSingleLineTagParser("BasePath", &setMetaSingle{swspec, rxBasePath, setSwaggerBasePath}), newSingleLineTagParser("Contact", &setMetaSingle{swspec, rxContact, setInfoContact}), newSingleLineTagParser("License", &setMetaSingle{swspec, rxLicense, setInfoLicense}), } return sp }
func setSwaggerBasePath(swspec *spec.Swagger, lines []string) error { var ln string if len(lines) > 0 { ln = lines[0] } swspec.BasePath = ln return nil }
func setSwaggerHost(swspec *spec.Swagger, lines []string) error { lns := lines if len(lns) == 0 || (len(lines) == 1 && len(lines[0]) == 0) { lns = []string{"localhost"} } swspec.Host = lns[0] return nil }
func saveSchema(spec *swspec.Swagger, name string, schema *swspec.Schema) { if schema == nil { return } if spec.Definitions == nil { spec.Definitions = make(map[string]swspec.Schema, 150) } spec.Definitions[name] = *schema }
func TestGenerateSwaggerYAML(t *testing.T) { seed := spec.Swagger{} seed.Host = "testapi.my" seed.Produces = []string{"application/json"} seed.Consumes = []string{"application/json"} seed.Schemes = []string{"http"} seed.Info = &spec.Info{} seed.Info.Description = "Our very little example API with 2 endpoints" seed.Info.Title = "Example API" seed.Info.Version = "0.1" seed.BasePath = "/" generator := NewSwaggerGeneratorYAML(seed) tests := getTests() doc, err := generator.Generate(tests) assert.NoError(t, err, "could not generate docs") // checking validity of generated swagger doc yamlMap := map[interface{}]interface{}{} err = yaml.Unmarshal(doc, &yamlMap) assert.NoError(t, err, "could not unmarshal generated doc into map") rawJSON, err := fmts.YAMLToJSON(yamlMap) assert.NoError(t, err) swaggerDoc, err := loads.Analyzed(rawJSON, "") assert.NoError(t, err) err = validate.Spec(swaggerDoc, strfmt.Default) assert.NoError(t, err) // checking equality of generated and expected doc actual := map[interface{}]interface{}{} err = yaml.Unmarshal(doc, &actual) assert.NoError(t, err, "could not unmarshal generated doc into map") fixture, err := ioutil.ReadFile("fixtures/swagger/swagger.yml") assert.NoError(t, err, "could not read fixture file") expected := map[interface{}]interface{}{} err = yaml.Unmarshal(fixture, &expected) assert.NoError(t, err, "could not unmarshal fixture into map") assert.Equal(t, expected, actual) }
func generateSwaggerYAML(t *testing.T, tests []apitest.IApiTest, writer io.Writer) { seed := spec.Swagger{} seed.Host = "localhost" seed.Produces = []string{"application/json"} seed.Consumes = []string{"application/json"} seed.Schemes = []string{"http"} seed.Info = &spec.Info{} seed.Info.Description = "Example API" seed.Info.Title = "Example API" seed.Info.Version = "0.1" seed.BasePath = "/" generator := apitest.NewSwaggerGeneratorYAML(seed) doc, err := generator.Generate(tests) if err != nil { t.Fatalf("could not generate doc: %s", err.Error()) } fmt.Fprint(writer, string(doc)) }
// Execute this command func (s *Spec) Execute(args []string) error { targetPath := "." if len(args) > 0 { targetPath = args[0] } realPath, err := filepath.Abs(targetPath) if err != nil { return err } var file *os.File switch s.Format { case "json": file, err = os.Create(filepath.Join(realPath, "swagger.json")) if err != nil { return err } case "yaml", "yml": file, err = os.Create(filepath.Join(realPath, "swagger.yml")) if err != nil { return err } default: return fmt.Errorf("invalid format: %s", s.Format) } defer file.Close() log.Println("creating specification document in", filepath.Join(targetPath, file.Name())) var doc spec.Swagger info := new(spec.Info) doc.Info = info doc.Swagger = "2.0" doc.Paths = new(spec.Paths) doc.Definitions = make(spec.Definitions) info.Title = s.Title if info.Title == "" { info.Title = swag.ToHumanNameTitle(filepath.Base(realPath)) } info.Description = s.Description info.Version = s.Version info.TermsOfService = s.Terms if s.Contact.Name != "" || s.Contact.Email != "" || s.Contact.URL != "" { var contact spec.ContactInfo contact.Name = s.Contact.Name contact.Email = s.Contact.Email contact.URL = s.Contact.URL info.Contact = &contact } if s.License.Name != "" || s.License.URL != "" { var license spec.License license.Name = s.License.Name license.URL = s.License.URL info.License = &license } for _, cons := range s.Consumes { doc.Consumes = append(doc.Consumes, cons) } for _, prods := range s.Produces { doc.Produces = append(doc.Produces, prods) } for _, scheme := range s.Schemes { doc.Schemes = append(doc.Schemes, scheme) } if s.Format == "json" { enc := json.NewEncoder(file) if err := enc.Encode(doc); err != nil { return err } return nil } b, err := yaml.Marshal(swag.ToDynamicJSON(doc)) if err != nil { return err } if _, err := file.Write(b); err != nil { return err } return nil }
func metaSecuritySetter(meta *spec.Swagger) func([]map[string][]string) { return func(secDefs []map[string][]string) { meta.Security = secDefs } }
func metaSchemeSetter(meta *spec.Swagger) func([]string) { return func(schemes []string) { meta.Schemes = schemes } }
func metaProducesSetter(meta *spec.Swagger) func([]string) { return func(produces []string) { meta.Produces = produces } }
func metaConsumesSetter(meta *spec.Swagger) func([]string) { return func(consumes []string) { meta.Consumes = consumes } }
func safeInfo(swspec *spec.Swagger) *spec.Info { if swspec.Info == nil { swspec.Info = new(spec.Info) } return swspec.Info }