...

Source file src/github.com/go-openapi/spec/properties_test.go

Documentation: github.com/go-openapi/spec

     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  
    15  package spec
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestPropertySerialization(t *testing.T) {
    22  	strProp := StringProperty()
    23  	strProp.Enum = append(strProp.Enum, "a", "b")
    24  
    25  	prop := &Schema{SchemaProps: SchemaProps{
    26  		Items: &SchemaOrArray{Schemas: []Schema{
    27  			{SchemaProps: SchemaProps{Type: []string{"string"}}},
    28  			{SchemaProps: SchemaProps{Type: []string{"string"}}},
    29  		}},
    30  	}}
    31  
    32  	var propSerData = []struct {
    33  		Schema *Schema
    34  		JSON   string
    35  	}{
    36  		{BooleanProperty(), `{"type":"boolean"}`},
    37  		{DateProperty(), `{"type":"string","format":"date"}`},
    38  		{DateTimeProperty(), `{"type":"string","format":"date-time"}`},
    39  		{Float64Property(), `{"type":"number","format":"double"}`},
    40  		{Float32Property(), `{"type":"number","format":"float"}`},
    41  		{Int32Property(), `{"type":"integer","format":"int32"}`},
    42  		{Int64Property(), `{"type":"integer","format":"int64"}`},
    43  		{MapProperty(StringProperty()), `{"type":"object","additionalProperties":{"type":"string"}}`},
    44  		{MapProperty(Int32Property()), `{"type":"object","additionalProperties":{"type":"integer","format":"int32"}}`},
    45  		{RefProperty("Dog"), `{"$ref":"Dog"}`},
    46  		{StringProperty(), `{"type":"string"}`},
    47  		{strProp, `{"type":"string","enum":["a","b"]}`},
    48  		{ArrayProperty(StringProperty()), `{"type":"array","items":{"type":"string"}}`},
    49  		{prop, `{"items":[{"type":"string"},{"type":"string"}]}`},
    50  	}
    51  
    52  	for _, v := range propSerData {
    53  		t.Log("roundtripping for", v.JSON)
    54  		assertSerializeJSON(t, v.Schema, v.JSON)
    55  		assertParsesJSON(t, v.JSON, v.Schema)
    56  	}
    57  
    58  }
    59  

View as plain text