properties_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spec
  15. import (
  16. "testing"
  17. )
  18. func TestPropertySerialization(t *testing.T) {
  19. strProp := StringProperty()
  20. strProp.Enum = append(strProp.Enum, "a", "b")
  21. prop := &Schema{SchemaProps: SchemaProps{
  22. Items: &SchemaOrArray{Schemas: []Schema{
  23. {SchemaProps: SchemaProps{Type: []string{"string"}}},
  24. {SchemaProps: SchemaProps{Type: []string{"string"}}},
  25. }},
  26. }}
  27. var propSerData = []struct {
  28. Schema *Schema
  29. JSON string
  30. }{
  31. {BooleanProperty(), `{"type":"boolean"}`},
  32. {DateProperty(), `{"type":"string","format":"date"}`},
  33. {DateTimeProperty(), `{"type":"string","format":"date-time"}`},
  34. {Float64Property(), `{"type":"number","format":"double"}`},
  35. {Float32Property(), `{"type":"number","format":"float"}`},
  36. {Int32Property(), `{"type":"integer","format":"int32"}`},
  37. {Int64Property(), `{"type":"integer","format":"int64"}`},
  38. {MapProperty(StringProperty()), `{"type":"object","additionalProperties":{"type":"string"}}`},
  39. {MapProperty(Int32Property()), `{"type":"object","additionalProperties":{"type":"integer","format":"int32"}}`},
  40. {RefProperty("Dog"), `{"$ref":"Dog"}`},
  41. {StringProperty(), `{"type":"string"}`},
  42. {strProp, `{"type":"string","enum":["a","b"]}`},
  43. {ArrayProperty(StringProperty()), `{"type":"array","items":{"type":"string"}}`},
  44. {prop, `{"items":[{"type":"string"},{"type":"string"}]}`},
  45. }
  46. for _, v := range propSerData {
  47. t.Log("roundtripping for", v.JSON)
  48. assertSerializeJSON(t, v.Schema, v.JSON)
  49. assertParsesJSON(t, v.JSON, v.Schema)
  50. }
  51. }