swaggest / rest

Web services with OpenAPI and JSON Schema done quick in Go
https://pkg.go.dev/github.com/swaggest/rest
MIT License
335 stars 17 forks source link

How to generate openapi.json file only? #189

Closed futugyou closed 4 months ago

futugyou commented 4 months ago

How to generate openapi.json file without using http.ListenAndServe to start the service. I just want to use 'go run .' to generate an openapi.json file, not start a http service.

vearutop commented 4 months ago

You can do it with a function like this.

func DumpOpenAPISpec(fn string, s *web.Service) error {
    j, err := json.Marshal(s.OpenAPISchema())
    if err != nil {
        return err
    }

    return os.WriteFile(fn, j, 0600)
}
futugyou commented 4 months ago

You can do it with a function like this.

func DumpOpenAPISpec(fn string, s *web.Service) error {
  j, err := json.Marshal(s.OpenAPISchema())
  if err != nil {
      return err
  }

  return os.WriteFile(fn, j, 0600)
}

thank you for your help