{"record":{"id":"49abcc556c713f22","repo":"goharbor/harbor","slug":"the-metadata-of-resource-cannot-be-null-49abcc","errorCode":null,"errorMessage":"the metadata of resource cannot be null","messagePattern":"the metadata of resource cannot be null","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/pkg/reg/adapter/jfrog/adapter.go","lineNumber":123,"sourceCode":"}\n\nfunc newAdapter(registry *model.Registry) (adp.Adapter, error) {\n\treturn &adapter{\n\t\tAdapter:  native.NewAdapter(registry),\n\t\tregistry: registry,\n\t\tclient:   newClient(registry),\n\t}, nil\n}\n\n// PrepareForPush creates local docker repository in jfrog artifactory\nfunc (a *adapter) PrepareForPush(resources []*model.Resource) error {\n\tvar namespaces []string\n\tfor _, resource := range resources {\n\t\tif resource == nil {\n\t\t\treturn errors.New(\"the resource cannot be null\")\n\t\t}\n\t\tif resource.Metadata == nil {\n\t\t\treturn errors.New(\"the metadata of resource cannot be null\")\n\t\t}\n\t\tif resource.Metadata.Repository == nil {\n\t\t\treturn errors.New(\"the namespace of resource cannot be null\")\n\t\t}\n\t\tif len(resource.Metadata.Repository.Name) == 0 {\n\t\t\treturn errors.New(\"the name of namespace cannot be null\")\n\t\t}\n\t\tpath := strings.Split(resource.Metadata.Repository.Name, \"/\")\n\t\tif len(path) > 0 {\n\t\t\tnamespaces = append(namespaces, path[0])\n\t\t}\n\t}\n\n\trepositories, err := a.listAllRepositories()\n\tif err != nil {\n\t\treturn err\n\t}\n\texistedRepositories := make(map[string]struct{})","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/goharbor/harbor/blob/7b2fd08cc568955cca339afeefab27372840d936/src/pkg/reg/adapter/jfrog/adapter.go#L105-L141","documentation":"Returned by the JFrog Artifactory adapter's PrepareForPush (src/pkg/reg/adapter/jfrog/adapter.go:123) when a resource in the slice is non-nil but its Metadata field is nil. The adapter needs resource.Metadata.Repository to derive the Artifactory repository (namespace) to pre-create, so a missing Metadata struct aborts the whole push preparation.","triggerScenarios":"Passing a *model.Resource constructed as &model.Resource{...} without the Metadata field set — e.g. only Type or Deleted flags populated — to PrepareForPush on the jfrog adapter.","commonSituations":"Building partial Resource objects in tests or custom copiers; code that constructs a resource for deletion (where Metadata may be omitted) and reuses the same path for push; refactors that move Metadata population into an optional branch.","solutions":["Always populate resource.Metadata (&model.ResourceMetadata{}) when building push resources; only the nil-resource case is tolerated less strictly than this.","Add a pre-flight validation pass that rejects nil Metadata with the element index (see validationCode).","Check the construction site: grep for 'model.Resource{' in your code and confirm every push-path literal includes Metadata.","In tests, use the same builders production code uses instead of hand-rolled literals."],"exampleFix":"// before\nres := &model.Resource{\n    Type: model.ResourceTypeImage,\n}\n\n// after\nres := &model.Resource{\n    Type:     model.ResourceTypeImage,\n    Metadata: &model.ResourceMetadata{\n        Repository: &model.Repository{Name: \"docker-local/ubuntu\"},\n    },\n}","handlingStrategy":"validation","validationCode":"for i, r := range resources {\n\tif r == nil || r.Metadata == nil {\n\t\treturn fmt.Errorf(\"resources[%d]: nil resource or metadata\", i)\n\t}\n}","typeGuard":"func hasMetadata(r *model.Resource) bool {\n\treturn r != nil && r.Metadata != nil\n}","tryCatchPattern":"if err := a.PrepareForPush(resources); err != nil {\n\tif strings.Contains(err.Error(), \"metadata of resource cannot be null\") {\n\t\t// fix construction site: resource literals must include Metadata\n\t}\n\treturn err\n}","preventionTips":["Require Metadata in the resource builder used by both prod and tests.","Do not reuse deletion-shaped (Metadata-less) resources on push paths.","Assert invariants in builder unit tests."],"tags":["go","jfrog","artifactory","replication","validation","nil-check"],"backgroundTag":null,"analyzedSha":"7b2fd08cc568955cca339afeefab27372840d936","analyzedAt":"2026-08-16T00:00:10.961Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}