Skip to content

[WIP] Schema Object and Array types #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build-map-keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ const (
keyParameters = "parameters"
keyRequired = "required"
keySchema = "schema"
keyItems = "items"
)
56 changes: 45 additions & 11 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -255,7 +272,7 @@ func makePropertiesMap(properties *SchemaProperties) map[string]interface{} {
propMap[keyDefault] = prop.Default
}

propertiesMap[prop.Name] = propMap
// }
}

return propertiesMap
Expand All @@ -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
Expand Down Expand Up @@ -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
}
25 changes: 17 additions & 8 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down