Documentation ¶
Index ¶
- Constants
- func ReadDictionary(dictionaryLocation string) []string
- type MockGenerator
- type MockType
- type SchemaRenderer
- func (wr *SchemaRenderer) DisableRequiredCheck()
- func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, structure map[string]any, depth int)
- func (wr *SchemaRenderer) PseudoUUID() string
- func (wr *SchemaRenderer) RandomFloat64() float64
- func (wr *SchemaRenderer) RandomInt(min, max int64) int64
- func (wr *SchemaRenderer) RandomWord(min, max int64, depth int) string
- func (wr *SchemaRenderer) RenderSchema(schema *base.Schema) any
Examples ¶
Constants ¶
const ( Example = "Example" Examples = "Examples" Schema = "Schema" )
Variables ¶
This section is empty.
Functions ¶
func ReadDictionary ¶
ReadDictionary will read a dictionary file and return a slice of strings.
Types ¶
type MockGenerator ¶
type MockGenerator struct {
// contains filtered or unexported fields
}
MockGenerator is used to generate mocks for high-level mockable structs or *base.Schema pointers. The mock generator will attempt to generate a mock from a struct using the following fields:
- Example: any type, this is the default example to use if no examples are present.
- Examples: *orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
- Schema: *base.SchemaProxy, this is the schema to use if no examples are present.
The mock generator will attempt to generate a mock from a *base.Schema pointer. Use NewMockGenerator or NewMockGeneratorWithDictionary to create a new mock generator.
Example (GenerateBurgerMock_yaml) ¶
// create a new YAML mock generator mg := NewMockGenerator(YAML) burgerShop, _ := os.ReadFile("../test_specs/burgershop.openapi.yaml") // create a new document from specification and build a v3 model. document, _ := libopenapi.NewDocument(burgerShop) v3Model, _ := document.BuildV3Model() // create a mock of the Burger model burgerModel := v3Model.Model.Components.Schemas.GetOrZero("Burger") burger := burgerModel.Schema() mock, err := mg.GenerateMock(burger, "") if err != nil { panic(err) } fmt.Println(string(mock))
Output: name: Big Mac numPatties: 2
Example (GenerateFriesMock_json) ¶
// create a new YAML mock generator mg := NewMockGenerator(JSON) burgerShop, _ := os.ReadFile("../test_specs/burgershop.openapi.yaml") // create a new document from specification and build a v3 model. document, _ := libopenapi.NewDocument(burgerShop) v3Model, _ := document.BuildV3Model() // create a mock of the Fries model friesModel := v3Model.Model.Components.Schemas.GetOrZero("Fries") fries := friesModel.Schema() mock, err := mg.GenerateMock(fries, "") if err != nil { panic(err) } fmt.Println(string(mock))
Output: {"favoriteDrink":{"drinkType":"coke","size":"M"},"potatoShape":"Crispy Shoestring"}
Example (GeneratePolymorphicMock_json) ¶
mg := NewMockGenerator(JSON) // create a new YAML mock generator burgerShop, _ := os.ReadFile("../test_specs/burgershop.openapi.yaml") // create a new document from specification and build a v3 model. document, _ := libopenapi.NewDocument(burgerShop) v3Model, _ := document.BuildV3Model() // create a mock of the SomePayload component, which uses polymorphism (incorrectly) payloadModel := v3Model.Model.Components.Schemas.GetOrZero("SomePayload") payload := payloadModel.Schema() mock, err := mg.GenerateMock(payload, "") if err != nil { panic(err) } fmt.Println(string(mock))
Output: {"drinkType":"coke","size":"M"}
Example (GenerateRequestMock_json) ¶
// create a new YAML mock generator mg := NewMockGenerator(JSON) burgerShop, _ := os.ReadFile("../test_specs/burgershop.openapi.yaml") // create a new document from specification and build a v3 model. document, _ := libopenapi.NewDocument(burgerShop) v3Model, _ := document.BuildV3Model() // create a mock of the burger request model, extracted from the operation directly. burgerRequestModel := v3Model.Model.Paths.PathItems.GetOrZero("/burgers"). Post.RequestBody.Content.GetOrZero("application/json") // use the 'cakeBurger' example to generate a mock mock, err := mg.GenerateMock(burgerRequestModel, "cakeBurger") if err != nil { panic(err) } fmt.Println(string(mock))
Output: {"name":"Chocolate Cake Burger","numPatties":5}
Example (GenerateResponseMock_json) ¶
mg := NewMockGenerator(JSON) // create a new YAML mock generator burgerShop, _ := os.ReadFile("../test_specs/burgershop.openapi.yaml") // create a new document from specification and build a v3 model. document, _ := libopenapi.NewDocument(burgerShop) v3Model, _ := document.BuildV3Model() // create a mock of the burger response model, extracted from the operation directly. burgerResponseModel := v3Model.Model.Paths.PathItems.GetOrZero("/burgers"). Post.Responses.Codes.GetOrZero("200").Content.GetOrZero("application/json") // use the 'filetOFish' example to generate a mock mock, err := mg.GenerateMock(burgerResponseModel, "filetOFish") if err != nil { panic(err) } fmt.Println(string(mock))
Output: {"name":"Filet-O-Fish","numPatties":1}
func NewMockGenerator ¶
func NewMockGenerator(mockType MockType) *MockGenerator
NewMockGenerator creates a new mock generator using the default dictionary. The default is located at /usr/share/dict/words on most systems. Windows users will need to use NewMockGeneratorWithDictionary to specify a custom dictionary.
func NewMockGeneratorWithDictionary ¶
func NewMockGeneratorWithDictionary(dictionaryLocation string, mockType MockType) *MockGenerator
NewMockGeneratorWithDictionary creates a new mock generator using a custom dictionary. This is useful if you want to use a custom dictionary to generate mocks. The location of a text file with one word per line is expected.
func (*MockGenerator) GenerateMock ¶
func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error)
GenerateMock generates a mock for a given high-level mockable struct. The mockable struct must contain the following fields: Example: any type, this is the default example to use if no examples are present. Examples: *orderedmap.Map[string, *base.Example], this is a map of examples keyed by name. Schema: *base.SchemaProxy, this is the schema to use if no examples are present. The name parameter is optional, if provided, the mock generator will attempt to find an example with the given name. If no name is provided, the first example will be used.
func (*MockGenerator) SetPretty ¶
func (mg *MockGenerator) SetPretty()
SetPretty sets the pretty flag on the mock generator. If true, the mock will be rendered with indentation and newlines. If false, the mock will be rendered as a single line which is good for API responses. False is the default. This option only effects JSON mocks, there is no concept of pretty printing YAML.
type SchemaRenderer ¶
type SchemaRenderer struct {
// contains filtered or unexported fields
}
SchemaRenderer is a renderer that will generate random words, numbers and values based on a dictionary file. The dictionary is just a slice of strings that is used to generate random words.
func CreateRendererUsingDefaultDictionary ¶
func CreateRendererUsingDefaultDictionary() *SchemaRenderer
CreateRendererUsingDefaultDictionary will create a new SchemaRenderer using the default dictionary file. The default dictionary is located at /usr/share/dict/words on most systems. Windows users will need to use CreateRendererUsingDictionary to specify a custom dictionary.
func CreateRendererUsingDictionary ¶
func CreateRendererUsingDictionary(dictionaryLocation string) *SchemaRenderer
CreateRendererUsingDictionary will create a new SchemaRenderer using a custom dictionary file. The location of a text file with one word per line is expected.
func (*SchemaRenderer) DisableRequiredCheck ¶
func (wr *SchemaRenderer) DisableRequiredCheck()
DisableRequiredCheck will disable the required check when rendering a schema. This means that all properties will be rendered, not just the required ones. https://github.com/devniel93/libopenapi/issues/200
func (*SchemaRenderer) DiveIntoSchema ¶
func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, structure map[string]any, depth int)
DiveIntoSchema will dive into a schema and inject values from examples into a map. If there are no examples in the schema, then the renderer will attempt to generate a value based on the schema type, format and pattern.
func (*SchemaRenderer) PseudoUUID ¶
func (wr *SchemaRenderer) PseudoUUID() string
PseudoUUID will return a random UUID, it's not a real UUID, but it's good enough for mock /example data.
func (*SchemaRenderer) RandomFloat64 ¶
func (wr *SchemaRenderer) RandomFloat64() float64
RandomFloat64 will return a random float64 between 0 and 1.
func (*SchemaRenderer) RandomInt ¶
func (wr *SchemaRenderer) RandomInt(min, max int64) int64
RandomInt will return a random int between the min and max values.
func (*SchemaRenderer) RandomWord ¶
func (wr *SchemaRenderer) RandomWord(min, max int64, depth int) string
RandomWord will return a random word from the dictionary file between the min and max values. The depth is used to prevent a stack overflow, the maximum depth is 100 (anything more than this is probably a bug). set the values to 0 to return the first word returned, essentially ignore the min and max values.
func (*SchemaRenderer) RenderSchema ¶
func (wr *SchemaRenderer) RenderSchema(schema *base.Schema) any
RenderSchema takes a schema and renders it into an interface, ready to be converted to JSON or YAML.