swaggest / jsonschema-go

JSON Schema mapping for Go
https://pkg.go.dev/github.com/swaggest/jsonschema-go
MIT License
102 stars 13 forks source link

fix bug: generic types schema name #80

Closed fourcels closed 1 year ago

fourcels commented 1 year ago
package controllers
// Declare input port type.
type helloInput struct {
    Locale string `query:"locale" default:"en-US" pattern:"^[a-z]{2}-[A-Z]{2}$" enum:"ru-RU,en-US"`
    Name   string `path:"name" minLength:"3"` // Field tags define parameter location and JSON schema constraints.

    // Field tags of unnamed fields are applied to parent schema.
    // they are optional and can be used to disallow unknown parameters.
    // For non-body params, name tag must be provided explicitly.
    // E.g. here no unknown `query` and `cookie` parameters allowed,
    // unknown `header` params are ok.
    _ struct{} `query:"_" cookie:"_" additionalProperties:"false"`
}

// Declare output port type.
type helloOutput struct {
    Now     time.Time `header:"X-Now" json:"-"`
    Message string    `json:"message"`
}

type ApiRespone[T any] struct {
    Data *T `json:"data"`
}

func Hello() usecase.Interactor {
    messages := map[string]string{
        "en-US": "Hello, %s!",
        "ru-RU": "Привет, %s!",
    }

    // Create use case interactor with references to input/output types and interaction function.
    u := usecase.NewInteractor(func(ctx context.Context, input helloInput, output *ApiRespone[helloOutput]) error {
        msg, available := messages[input.Locale]
        if !available {
            return status.Wrap(errors.New("unknown locale"), status.InvalidArgument)
        }

        output.Data = &helloOutput{
            Message: fmt.Sprintf(msg, input.Name),
            Now:     time.Now(),
        }
        // output.Now = time.Now()

        return nil
    })

    // Describe use case interactor.
    u.SetTitle("Greeter")
    u.SetDescription("Greeter greets you.")

    u.SetExpectedErrors(status.InvalidArgument)
    return u
}
package main

func main() {
    s := web.DefaultService()

    // Init API documentation schema.
    s.OpenAPI.Info.Title = "Basic Example"
    s.OpenAPI.Info.WithDescription("This app showcases a trivial REST API.")
    s.OpenAPI.Info.Version = "v1.2.3"

    // Setup middlewares.
    s.Wrap(
        gzip.Middleware, // Response compression with support for direct gzip pass through.
    )

    // Add use case handler to router.
    s.Get("/hello/{name}", controllers.Hello())

    // Swagger UI endpoint at /docs.
    s.Docs("/docs", swgui.New)

    // Start server.
    log.Println("http://localhost:8011/docs")
    if err := http.ListenAndServe(":8011", s); err != nil {
        log.Fatal(err)
    }
}

codecov[bot] commented 1 year ago

Codecov Report

Merging #80 (3de457f) into master (d4b7536) will increase coverage by 0.02%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #80      +/-   ##
==========================================
+ Coverage   73.33%   73.35%   +0.02%     
==========================================
  Files           6        6              
  Lines        1065     1066       +1     
==========================================
+ Hits          781      782       +1     
  Misses        216      216              
  Partials       68       68              
Flag Coverage Δ
unittests 73.35% <100.00%> (+0.02%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
reflect.go 82.33% <100.00%> (+0.03%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more