From 8f992da7ebd00a230706e8713ae6c2d73aa32774 Mon Sep 17 00:00:00 2001 From: Patricio Diaz Date: Tue, 4 Apr 2023 11:48:31 -0400 Subject: [PATCH 1/3] Initial work --- build.go | 32 +++++++++++++++++++++----------- models.go | 14 ++++++++------ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/build.go b/build.go index 8bae031..88f1187 100644 --- a/build.go +++ b/build.go @@ -239,20 +239,30 @@ func makePropertiesMap(properties *SchemaProperties) map[string]interface{} { propMap[keyType] = prop.Type } - if !isStrEmpty(prop.Format) { - propMap[keyFormat] = prop.Format - } + if prop.Ref != "" { + if prop.Ref != "" { + propMap[keyRef] = prop.Ref + } + } else { + if !isStrEmpty(prop.Format) { + propMap[keyFormat] = prop.Format + } - if !isStrEmpty(prop.Description) { - propMap[keyDescription] = prop.Description - } + if !isStrEmpty(prop.Description) { + propMap[keyDescription] = prop.Description + } - if len(prop.Enum) > 0 { - propMap[keyEnum] = prop.Enum - } + if len(prop.Enum) > 0 { + propMap[keyEnum] = prop.Enum + } - if prop.Default != nil { - propMap[keyDefault] = prop.Default + if prop.Default != nil { + propMap[keyDefault] = prop.Default + } + + if prop.Properties != nil { + propMap[keyProperties] = makePropertiesMap(prop.Properties) + } } propertiesMap[prop.Name] = propMap diff --git a/models.go b/models.go index 309789b..57f6703 100644 --- a/models.go +++ b/models.go @@ -173,12 +173,14 @@ 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"` } // SecuritySchemes is a slice of SecuritySchemes objects. From 7ea59a7543d18e1a40834816719a95903364a373 Mon Sep 17 00:00:00 2001 From: Patricio Diaz Date: Wed, 5 Apr 2023 00:52:03 -0400 Subject: [PATCH 2/3] WIP --- build-map-keys.go | 1 + build.go | 88 ++++++++++++++++++++++++++++++----------------- models.go | 11 ++++-- 3 files changed, 66 insertions(+), 34 deletions(-) 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 88f1187..ff9216b 100644 --- a/build.go +++ b/build.go @@ -235,37 +235,44 @@ 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.Description) { + propMap[keyDescription] = prop.Description + } + if prop.Ref != "" { - if prop.Ref != "" { - propMap[keyRef] = prop.Ref - } - } else { - if !isStrEmpty(prop.Format) { - propMap[keyFormat] = prop.Format - } - - if !isStrEmpty(prop.Description) { - propMap[keyDescription] = prop.Description - } - - if len(prop.Enum) > 0 { - propMap[keyEnum] = prop.Enum - } - - if prop.Default != nil { - propMap[keyDefault] = prop.Default - } - - if prop.Properties != nil { - propMap[keyProperties] = makePropertiesMap(prop.Properties) - } + propMap[keyRef] = prop.Ref + continue } - propertiesMap[prop.Name] = propMap + 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 + } + + if prop.Default != nil { + propMap[keyDefault] = prop.Default + } + + // } } return propertiesMap @@ -279,15 +286,22 @@ func makeComponentSchemasMap(schemas *Schemas) map[string]interface{} { if s.Ref != "" { scheme[keyRef] = s.Ref - } else { - scheme[keyType] = s.Type - schemesMap[s.Name] = scheme - scheme[keyProperties] = makePropertiesMap(&s.Properties) - - if s.XML.Name != "" { - scheme[keyXML] = s.XML - } + continue + } + + if s.Items != nil { + scheme[keyItems] = makeItemsMap(s.Items) + continue } + + scheme[keyType] = s.Type + scheme[keyProperties] = makePropertiesMap(&s.Properties) + + if s.XML.Name != "" { + scheme[keyXML] = s.XML + } + + schemesMap[s.Name] = scheme } return schemesMap @@ -397,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 57f6703..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. @@ -181,6 +182,12 @@ type SchemaProperty struct { 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. From 926b9661c2323acb23efe45a0386d6bd2053913f Mon Sep 17 00:00:00 2001 From: Patricio Diaz Date: Fri, 5 May 2023 12:35:53 -0400 Subject: [PATCH 3/3] Added support for Array type --- build.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build.go b/build.go index ff9216b..8d9326f 100644 --- a/build.go +++ b/build.go @@ -283,19 +283,19 @@ 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 - continue } if s.Items != nil { scheme[keyItems] = makeItemsMap(s.Items) - continue } - scheme[keyType] = s.Type - scheme[keyProperties] = makePropertiesMap(&s.Properties) + if s.Properties != nil { + scheme[keyProperties] = makePropertiesMap(&s.Properties) + } if s.XML.Name != "" { scheme[keyXML] = s.XML