Skip to content

feat!: support typed map #242

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

Merged
merged 2 commits into from
Feb 5, 2025
Merged
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
4 changes: 4 additions & 0 deletions client_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ Organization:
- PermissionsUpdate
- UserOrganizationCreate
- UserOrganizationsList
OrganizationProjects:
- OrganizationProjectsCreate
- OrganizationProjectsDelete
- OrganizationProjectsList
- OrganizationProjectsUpdate
OrganizationUser:
- OrganizationUserAuthenticationMethodsList
- OrganizationUserDelete
Expand Down
32 changes: 30 additions & 2 deletions generator/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ const (
SchemaTypeAny = "any"
)

// anyKey This is what "schemaless" map looks like in the Aiven API
//
// {
// "type": "object",
// "properties": {
// "ANY": {
// "type": "string",
// }
// }
// }
const anyKey = "ANY"

// Schema represents a parsed OpenAPI schema.
type Schema struct {
Type SchemaType `json:"type"`
Expand Down Expand Up @@ -282,6 +294,18 @@ func (s *Schema) init(doc *Doc, scope map[string]*Schema, name string) {
s.Items.init(doc, scope, toSingle(name))
}

// See anyKey const
if s.isTypedMap() {
s.AdditionalProperties = s.Properties[anyKey]
s.AdditionalProperties.init(doc, scope, toSingle(name))
s.Properties = nil
}

// Removes pointers from map values
if s.AdditionalProperties != nil {
s.AdditionalProperties.required = true
}

if s.isObject() {
s.propertyNames = sortedKeys(s.Properties)
for _, k := range s.propertyNames {
Expand Down Expand Up @@ -359,6 +383,11 @@ func (s *Schema) isMap() bool {
return s.Type == SchemaTypeObject && len(s.Properties) == 0
}

// isTypedMap it has only one field with "ANY" key
func (s *Schema) isTypedMap() bool {
return s.Type == SchemaTypeObject && len(s.Properties) == 1 && s.Properties[anyKey] != nil
}

func (s *Schema) isEnum() bool {
return len(s.Enum) != 0
}
Expand Down Expand Up @@ -451,10 +480,9 @@ func getType(s *Schema) *jen.Statement {
}

return withPointer(o, s.required)
case s.isMap():
case s.isMap(), s.isTypedMap():
a := withPointer(jen.Map(jen.String()), s.required || s.isOut())
if s.AdditionalProperties != nil {
s.AdditionalProperties.required = true
return a.Add(getType(s.AdditionalProperties))
} else if s.name == "tags" {
// tags are everywhere in the schema, better not to use the patch
Expand Down
158 changes: 158 additions & 0 deletions handler/organizationprojects/organizationprojects.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading