Skip to content

Cannot convert an expression of the type 'map[string]SomeType' to the type 'SomeType' #2682

@SVilgelm

Description

@SVilgelm

Problem statement

short: swagger generates broken code for valid spec and go fails with such error:

Cannot convert an expression of the type 'map[string]AuthzV1RoleBindingsListAllResponseResult' to the type 'AuthzV1RoleBindingsListAllResponseResult'

Here I created the repo with spec and generated code: https://github.com/SVilgelm/go-swagger-generates-broken-code

Places with incorrect type conversions:

As you can see:

  • the AuthzV1RoleBindingsListAllResponse struct has Result field with type map[string]AuthzV1RoleBindingsListAllResponseResult
  • the AuthzV1RoleBindingsListAllResponseResult is the type of map[string][]AuthzV1RoleBindingConfig

so the Result is a map of map of list of config, it is totally correct.

But the AuthzV1RoleBindingsListAllResponse.validateResult tries to convert from map of map of list of config to map of list of config, one map level is lost

so, instead of

		if err := validate.Required("result"+"."+k, "body", AuthzV1RoleBindingsListAllResponseResult(m.Result)); err != nil {
			return err
		}

the code should be like this:

		if err := validate.Required("result"+"."+k, "body", map[string]AuthzV1RoleBindingsListAllResponseResult(m.Result)); err != nil {
			return err
		}

Spec: https://github.com/SVilgelm/go-swagger-generates-broken-code/blob/main/swagger.json

% swagger validate swagger.json 
2021/12/14 10:22:25 
The swagger spec at "swagger.json" is valid against swagger specification 2.0

the generated models can be found here: https://github.com/SVilgelm/go-swagger-generates-broken-code/tree/main/pkg/models
and log:

% swagger generate client -f swagger.json -c pkg/client -m pkg/models --default-scheme=https
2021/12/14 10:18:29 validating spec /Users/sergeyvilgelm/tmp/swagger.json
2021/12/14 10:18:30 preprocessing spec with option:  minimal flattening
2021/12/14 10:18:30 building a plan for generation
2021/12/14 10:18:30 generation target ./
2021/12/14 10:18:30 planning definitions (found: 3)
2021/12/14 10:18:30 planning operations (found: 1)
2021/12/14 10:18:30 grouping operations into packages (packages: 1)
2021/12/14 10:18:30 operations for package packages "operations" (found: 1)
2021/12/14 10:18:30 planning meta data and facades
2021/12/14 10:18:30 rendering 1 templates for model authz.v1.RoleBindingConfig
2021/12/14 10:18:30 name field authz.v1.RoleBindingConfig
2021/12/14 10:18:30 package field models
2021/12/14 10:18:30 creating generated file "authz_v1_role_binding_config.go" in "pkg/models" as definition
2021/12/14 10:18:30 executed template asset:model
2021/12/14 10:18:30 rendering 1 templates for model authz.v1.RoleBindingsListAllResponse
2021/12/14 10:18:30 name field authz.v1.RoleBindingsListAllResponse
2021/12/14 10:18:30 package field models
2021/12/14 10:18:30 creating generated file "authz_v1_role_bindings_list_all_response.go" in "pkg/models" as definition
2021/12/14 10:18:30 executed template asset:model
2021/12/14 10:18:30 rendering 1 templates for model authz.v1.RoleBindingsListAllResponse.result
2021/12/14 10:18:30 name field authz.v1.RoleBindingsListAllResponse.result
2021/12/14 10:18:30 package field models
2021/12/14 10:18:30 creating generated file "authz_v1_role_bindings_list_all_response_result.go" in "pkg/models" as definition
2021/12/14 10:18:30 executed template asset:model
2021/12/14 10:18:30 rendering 2 templates for operation 
2021/12/14 10:18:30 name field ListAllRoleBindings
2021/12/14 10:18:30 package field operations
2021/12/14 10:18:30 creating generated file "list_all_role_bindings_parameters.go" in "pkg/client/operations" as parameters
2021/12/14 10:18:30 executed template asset:clientParameter
2021/12/14 10:18:30 name field ListAllRoleBindings
2021/12/14 10:18:30 package field operations
2021/12/14 10:18:30 creating generated file "list_all_role_bindings_responses.go" in "pkg/client/operations" as responses
2021/12/14 10:18:30 executed template asset:clientResponse
2021/12/14 10:18:30 rendering 1 templates for operation group 
2021/12/14 10:18:30 name field operations
2021/12/14 10:18:30 creating generated file "operations_client.go" in "pkg/client/operations" as client
2021/12/14 10:18:30 executed template asset:clientClient
2021/12/14 10:18:30 rendering 1 templates for application Rest
2021/12/14 10:18:30 name field Rest
2021/12/14 10:18:30 package field client
2021/12/14 10:18:30 creating generated file "rest_client.go" in "pkg/client" as facade
2021/12/14 10:18:30 executed template asset:clientFacade
2021/12/14 10:18:31 Generation completed!

Environment

swagger version: v0.28.0, commit: 48a66e4
go version: go1.17.5 darwin/amd64
OS: macOS Monterey 12.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions