diff --git a/build-map-keys.go b/build-map-keys.go index 4135aff..bafa782 100644 --- a/build-map-keys.go +++ b/build-map-keys.go @@ -26,4 +26,5 @@ const ( keyParameters = "parameters" keyRequired = "required" keySchema = "schema" + keyItems = "items" ) diff --git a/build.go b/build.go index 8bae031..8d9326f 100644 --- a/build.go +++ b/build.go @@ -235,18 +235,35 @@ func makePropertiesMap(properties *SchemaProperties) map[string]interface{} { for _, prop := range *properties { propMap := make(map[string]interface{}) + propertiesMap[prop.Name] = propMap + if !isStrEmpty(prop.Type) { propMap[keyType] = prop.Type } - if !isStrEmpty(prop.Format) { - propMap[keyFormat] = prop.Format - } - if !isStrEmpty(prop.Description) { propMap[keyDescription] = prop.Description } + if prop.Ref != "" { + propMap[keyRef] = prop.Ref + continue + } + + if prop.Items != nil { + propMap[keyItems] = makeItemsMap(prop.Items) + continue + } + + if prop.Properties != nil { + propMap[keyProperties] = makePropertiesMap(prop.Properties) + } + + // else { + if !isStrEmpty(prop.Format) { + propMap[keyFormat] = prop.Format + } + if len(prop.Enum) > 0 { propMap[keyEnum] = prop.Enum } @@ -255,7 +272,7 @@ func makePropertiesMap(properties *SchemaProperties) map[string]interface{} { propMap[keyDefault] = prop.Default } - propertiesMap[prop.Name] = propMap + // } } return propertiesMap @@ -266,18 +283,25 @@ func makeComponentSchemasMap(schemas *Schemas) map[string]interface{} { for _, s := range *schemas { scheme := make(map[string]interface{}) + scheme[keyType] = s.Type if s.Ref != "" { scheme[keyRef] = s.Ref - } else { - scheme[keyType] = s.Type - schemesMap[s.Name] = scheme + } + + if s.Items != nil { + scheme[keyItems] = makeItemsMap(s.Items) + } + + if s.Properties != nil { scheme[keyProperties] = makePropertiesMap(&s.Properties) + } - if s.XML.Name != "" { - scheme[keyXML] = s.XML - } + if s.XML.Name != "" { + scheme[keyXML] = s.XML } + + schemesMap[s.Name] = scheme } return schemesMap @@ -387,3 +411,13 @@ func makeSchemaMap(schema *Schema) map[string]interface{} { return schemaMap } + +func makeItemsMap(items *ArrayItems) map[string]interface{} { + itemsMap := make(map[string]interface{}) + + if !isStrEmpty(items.Ref) { + itemsMap[keyRef] = items.Ref + } + + return itemsMap +} diff --git a/models.go b/models.go index 309789b..4279bc4 100644 --- a/models.go +++ b/models.go @@ -159,8 +159,9 @@ type Schema struct { Name string Type string Properties SchemaProperties - XML XMLEntry `yaml:"xml, omitempty"` - Ref string // $ref: '#/components/schemas/Pet' // TODO: Should this be omitted if empty? + XML XMLEntry `yaml:"xml, omitempty"` + Ref string `yaml:"$ref,omitempty"` // $ref: '#/components/schemas/Pet' // TODO: Should this be omitted if empty? + Items *ArrayItems `yaml:"items,omitempty"` } // XMLEntry represents name of XML entry in Schema object. @@ -173,12 +174,20 @@ type SchemaProperties []SchemaProperty // SchemaProperty represents OAS schema object, used by Schema. type SchemaProperty struct { - Name string `yaml:"-"` - Type string // OAS3.0 data types - e.g. integer, boolean, string - Format string `yaml:"format,omitempty"` - Description string `yaml:"description,omitempty"` - Enum []string `yaml:"enum,omitempty"` - Default interface{} `yaml:"default,omitempty"` + Name string `yaml:"-"` + Type string // OAS3.0 data types - e.g. integer, boolean, string + Format string `yaml:"format,omitempty"` + Description string `yaml:"description,omitempty"` + Ref string `yaml:"$ref,omitempty"` + Enum []string `yaml:"enum,omitempty"` + Default interface{} `yaml:"default,omitempty"` + Properties *SchemaProperties `yaml:"properties,omitempty"` + Items *ArrayItems `yaml:"items,omitempty"` +} + +type ArrayItems struct { + Properties *SchemaProperties `yaml:"properties,omitempty"` + Ref string `yaml:"$ref,omitempty"` } // SecuritySchemes is a slice of SecuritySchemes objects.