Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use all property examples in generated mock responses #124

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cmd/root_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var (

// mock mode
var mockMode bool
var useAllMockResponseFields bool

certFlag, _ := cmd.Flags().GetString("cert")
if certFlag != "" {
Expand All @@ -82,6 +83,7 @@ var (

debug, _ := cmd.Flags().GetBool("debug")
mockMode, _ = cmd.Flags().GetBool("mock-mode")
useAllMockResponseFields, _ = cmd.Flags().GetBool("enable-all-mock-response-fields")
hardError, _ = cmd.Flags().GetBool("hard-validation")
hardErrorCode, _ = cmd.Flags().GetInt("hard-validation-code")
hardErrorReturnCode, _ = cmd.Flags().GetInt("hard-validation-return-code")
Expand Down Expand Up @@ -182,6 +184,11 @@ var (
config.MockMode = true
}
}
if useAllMockResponseFields {
if !config.UseAllMockResponseFields {
config.UseAllMockResponseFields = true
}
}
if streamReport {
if !config.StreamReport {
config.StreamReport = true
Expand Down Expand Up @@ -217,6 +224,9 @@ var (
if mockMode {
config.MockMode = true
}
if useAllMockResponseFields {
config.UseAllMockResponseFields = true
}
if streamReport {
config.StreamReport = true
}
Expand Down Expand Up @@ -654,6 +664,7 @@ func Execute(version, commit, date string, fs embed.FS) {
rootCmd.Flags().IntP("hard-validation-code", "q", 400, "Set a custom http error code for non-compliant requests when using the hard-error flag")
rootCmd.Flags().IntP("hard-validation-return-code", "y", 502, "Set a custom http error code for non-compliant responses when using the hard-error flag")
rootCmd.Flags().BoolP("mock-mode", "x", false, "Run in mock mode, responses are mocked and no traffic is sent to the target API (requires OpenAPI spec)")
rootCmd.Flags().BoolP("enable-all-mock-response-fields", "o", true, "Enable usage of all property examples in mock responses. When set to false, only required field examples will be used.")
rootCmd.Flags().StringP("config", "c", "", "Location of wiretap configuration file to use (default is .wiretap in current directory)")
rootCmd.Flags().StringP("base", "b", "", "Set a base path to resolve relative file references from, or a overriding base URL to resolve remote references from")
rootCmd.Flags().BoolP("debug", "l", false, "Enable debug logging")
Expand Down
3 changes: 2 additions & 1 deletion daemon/wiretap_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func NewWiretapService(document libopenapi.Document, config *shared.WiretapConfi
}

// create a new mock engine
wts.mockEngine = mock.NewMockEngine(wts.docModel, config.MockModePretty)
wts.mockEngine = mock.NewMockEngine(wts.docModel, config.MockModePretty,
config.UseAllMockResponseFields)

// hard-wire the config, change this later if needed.
wts.config = config
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/uuid v1.6.0
github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb // indirect
github.com/pb33f/harhar v0.0.0-20240111233202-e393c2a39a60
github.com/pb33f/libopenapi v0.16.6
github.com/pb33f/libopenapi v0.16.8
github.com/pb33f/libopenapi-validator v0.0.56
github.com/pb33f/ranch v0.4.0
github.com/pterm/pterm v0.12.79
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/harhar v0.0.0-20240111233202-e393c2a39a60 h1:5Nt5F1BxaYfbLolyD0kIflqoOOnzZlp8rmvZL93HbDc=
github.com/pb33f/harhar v0.0.0-20240111233202-e393c2a39a60/go.mod h1:sJHiXr/Rdkg2j8P5FYGEcqPP1ptdk8zn6xeJ3aZfAvw=
github.com/pb33f/libopenapi v0.16.6 h1:w0qrbAziEVvUlzGP7rTjMYS8bv9CfuIOW8OTOBgFGuo=
github.com/pb33f/libopenapi v0.16.6/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi v0.16.8 h1:EceJ+JTHViwjKNyP3LJ8J39jSQ4F2oOvHw6KfWCcVxM=
github.com/pb33f/libopenapi v0.16.8/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi-validator v0.0.56 h1:kYO8DRsSRpnxhUExuEfsgARuT/hkc/ISC77CX0BMze4=
github.com/pb33f/libopenapi-validator v0.0.56/go.mod h1:Cwdf0GUmnUrbw1LBpcGV1Wx5q/LC9akREV4Jc6/yJDc=
github.com/pb33f/ranch v0.4.0 h1:fve9Lozc9m0IGkygWD3EFkBtbRua+2gyi9yxYHvufh4=
Expand Down
7 changes: 6 additions & 1 deletion mock/mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ type ResponseMockEngine struct {
pretty bool
}

func NewMockEngine(document *v3.Document, pretty bool) *ResponseMockEngine {
func NewMockEngine(document *v3.Document, pretty, useAllPropertyExamples bool) *ResponseMockEngine {
me := renderer.NewMockGenerator(renderer.JSON)
if pretty {
me.SetPretty()
}

if useAllPropertyExamples {
me.DisableRequiredCheck()
}

return &ResponseMockEngine{
doc: document,
validator: validation.NewHttpValidator(document),
Expand Down
Loading
Loading