Skip to content

Commit

Permalink
fixes 3316 - disable examples globally (#3330)
Browse files Browse the repository at this point in the history
dix

set Randomizer to nil in New function

check for nil Randomizer

document behaviour for Randomizer set to nil

updated comment for randomizer

fix

removed new lines

fix

Co-authored-by: Francesco Cartier <[email protected]>
  • Loading branch information
antipopp and Francesco Cartier authored Aug 30, 2023
1 parent cf3760b commit b11ee3c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions expr/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (a *AttributeExpr) Example(r *ExampleGenerator) any {
return ex[len(ex)-1].Value
}

if r.Randomizer == nil {
return nil
}

value, ok := a.Meta.Last("openapi:example")
if !ok {
value, ok = a.Meta.Last("swagger:example")
Expand Down
2 changes: 2 additions & 0 deletions expr/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
//
// The random values should be consistent in that given the same seed the same
// random values get generated.
//
// Setting the randomizer to nil disables example generation.
type Randomizer interface {
// ArrayLength decides how long an example array will be
ArrayLength() int
Expand Down
8 changes: 8 additions & 0 deletions http/codegen/openapi/v3/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func New(root *expr.RootExpr) *OpenAPI {
return nil
}

m, ok := root.API.Meta.Last("openapi:example")
if !ok {
m, ok = root.API.Meta.Last("swagger:example")
}
if ok && m == "false" {
root.API.ExampleGenerator.Randomizer = nil
}

var (
bodies, types = buildBodyTypes(root.API)

Expand Down

0 comments on commit b11ee3c

Please sign in to comment.