Skip to content

Commit

Permalink
Added support for decimal and bigint format schema rendering
Browse files Browse the repository at this point in the history
Want big numbers? you got it. pb33f/wiretap#93
  • Loading branch information
daveshanley committed May 1, 2024
1 parent b8f8157 commit e6ccd8b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
32 changes: 31 additions & 1 deletion renderer/schema_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
stringType = "string"
numberType = "number"
integerType = "integer"
bigIntType = "bigint"
decimalType = "decimal"
booleanType = "boolean"
objectType = "object"
arrayType = "array"
Expand Down Expand Up @@ -217,7 +219,10 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
}

// handle numbers
if slices.Contains(schema.Type, numberType) || slices.Contains(schema.Type, integerType) {
if slices.Contains(schema.Type, numberType) ||
slices.Contains(schema.Type, integerType) ||
slices.Contains(schema.Type, bigIntType) ||
slices.Contains(schema.Type, decimalType) {

if schema.Enum != nil && len(schema.Enum) > 0 {
enum := schema.Enum[rand.Int()%len(schema.Enum)]
Expand All @@ -238,13 +243,38 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
maximum = int64(*schema.Maximum)
}

if schema.Example != nil {
var example any
_ = schema.Example.Decode(&example)
structure[key] = example
return

Check warning on line 250 in renderer/schema_renderer.go

View check run for this annotation

Codecov / codecov/patch

renderer/schema_renderer.go#L247-L250

Added lines #L247 - L250 were not covered by tests
}
if schema.Examples != nil {
if len(schema.Examples) > 0 {
renderedExamples := make([]any, len(schema.Examples))
for i, exmp := range schema.Examples {
if exmp != nil {
var ex any
_ = exmp.Decode(&ex)
renderedExamples[i] = fmt.Sprint(ex)

Check warning on line 259 in renderer/schema_renderer.go

View check run for this annotation

Codecov / codecov/patch

renderer/schema_renderer.go#L253-L259

Added lines #L253 - L259 were not covered by tests
}
}
structure[key] = renderedExamples
return

Check warning on line 263 in renderer/schema_renderer.go

View check run for this annotation

Codecov / codecov/patch

renderer/schema_renderer.go#L262-L263

Added lines #L262 - L263 were not covered by tests
}
}

switch schema.Format {
case floatType:
structure[key] = rand.Float32()
case doubleType:
structure[key] = rand.Float64()
case int32Type:
structure[key] = int(wr.RandomInt(minimum, maximum))
case bigIntType:
structure[key] = wr.RandomInt(minimum, maximum)

Check warning on line 275 in renderer/schema_renderer.go

View check run for this annotation

Codecov / codecov/patch

renderer/schema_renderer.go#L275

Added line #L275 was not covered by tests
case decimalType:
structure[key] = wr.RandomFloat64()

Check warning on line 277 in renderer/schema_renderer.go

View check run for this annotation

Codecov / codecov/patch

renderer/schema_renderer.go#L277

Added line #L277 was not covered by tests
default:
structure[key] = wr.RandomInt(minimum, maximum)
}
Expand Down
29 changes: 29 additions & 0 deletions renderer/schema_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,35 @@ properties:
assert.Equal(t, `{"args":{"arrParam":"test,test2","arrParamExploded":["1","2"]}}`, string(rendered))
}

// https://github.com/pb33f/wiretap/issues/93
func TestRenderSchema_NonStandard_Format(t *testing.T) {
testObject := `type: object
properties:
bigint:
type: integer
format: bigint
example: 8821239038968084
bigintStr:
type: string
format: bigint
example: "9223372036854775808"
decimal:
type: number
format: decimal
example: 3.141592653589793
decimalStr:
type: string
format: decimal
example: "3.14159265358979344719667586"`

compiled := getSchema([]byte(testObject))
schema := make(map[string]any)
wr := createSchemaRenderer()
wr.DiveIntoSchema(compiled, "pb33f", schema, 0)
rendered, _ := json.Marshal(schema["pb33f"])
assert.Equal(t, `{"bigint":8821239038968084,"bigintStr":"9223372036854775808","decimal":3.141592653589793,"decimalStr":"3.14159265358979344719667586"}`, string(rendered))
}

func TestCreateRendererUsingDefaultDictionary(t *testing.T) {
assert.NotNil(t, CreateRendererUsingDefaultDictionary())
}
Expand Down

0 comments on commit e6ccd8b

Please sign in to comment.