{"record":{"id":"79d9cb78b40fc590","repo":"Tencent/WeKnora","slug":"unmarshal-data-sources-w","errorCode":null,"errorMessage":"unmarshal data_sources: %w","messagePattern":"unmarshal data_sources: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/datasource/connector/notion/client.go","lineNumber":206,"sourceCode":"func (c *notionClient) GetDatabaseInfo(ctx context.Context, dbID string) (*databaseInfo, error) {\n\trespBody, err := c.doRequest(ctx, http.MethodGet, \"/v1/databases/\"+dbID, nil)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tvar db notionPage\n\tif err := json.Unmarshal(respBody, &db); err != nil {\n\t\treturn nil, fmt.Errorf(\"unmarshal database: %w\", err)\n\t}\n\tdb.Title = extractTitle(&db)\n\n\tvar dsResult struct {\n\t\tDataSources []struct {\n\t\t\tID string `json:\"id\"`\n\t\t} `json:\"data_sources\"`\n\t}\n\tif err := json.Unmarshal(respBody, &dsResult); err != nil {\n\t\treturn nil, fmt.Errorf(\"unmarshal data_sources: %w\", err)\n\t}\n\n\tdsID := \"\"\n\tif len(dsResult.DataSources) > 0 {\n\t\tdsID = dsResult.DataSources[0].ID\n\t}\n\n\treturn &databaseInfo{Page: db, DataSourceID: dsID}, nil\n}\n\n// GetDataSourceInfo retrieves a data source by ID, returning its metadata.\n// In API 2025-09-03+, data_source objects hold the schema/properties and are\n// the target for record queries. The response includes database_parent to\n// locate the database in the workspace hierarchy.\nfunc (c *notionClient) GetDataSourceInfo(ctx context.Context, dsID string) (*notionPage, error) {\n\trespBody, err := c.doRequest(ctx, http.MethodGet, \"/v1/data_sources/\"+dsID, nil)\n\tif err != nil {\n\t\treturn nil, err","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/datasource/connector/notion/client.go#L188-L224","documentation":"Second decode step inside GetDatabaseInfo: after the notionPage unmarshal succeeds, the same response body is unmarshaled into an anonymous struct with a `data_sources` array to extract the data source ID. Failure means the `data_sources` field is present with a non-array/non-object shape, or the body is otherwise incompatible. An empty/missing data_sources key does NOT error — dsID simply stays empty.","triggerScenarios":"GET /v1/databases/{id} with an API version that returns a differently-shaped data_sources field (e.g. string instead of array of objects, or objects without string id), so json.Unmarshal fails on the anonymous struct. Only reachable when the first notionPage unmarshal succeeded.","commonSituations":"Pinned Notion-Version older than 2025-09-03 where databases have no data_sources (usually yields empty, not error) vs newer versions changing the field type; proxy-rewritten responses; custom mock servers in tests returning incompatible fixtures.","solutions":["Check NotionAPIVersion — the data_sources array shape is from API version 2025-09-03+; align the pinned version with the expected schema.","Log respBody to compare the actual data_sources JSON type against the expected [{\"id\":\"...\"}].","If data_sources may legitimately be absent, treat this as optional rather than fatal — the code already handles empty arrays at client.go:210.","Update the anonymous struct at client.go:200 to match the actual API response shape."],"exampleFix":"// before: strict anonymous struct\nvar dsResult struct {\n    DataSources []struct {\n        ID string `json:\"id\"`\n    } `json:\"data_sources\"`\n}\n// after: tolerate raw and parse leniently\nvar dsResult struct {\n    DataSources []json.RawMessage `json:\"data_sources\"`\n}\n_ = json.Unmarshal(respBody, &dsResult) // optional field\nfor _, raw := range dsResult.DataSources {\n    var ds struct { ID string `json:\"id\"` }\n    if json.Unmarshal(raw, &ds) == nil && ds.ID != \"\" { dsID = ds.ID; break }\n}","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"info, err := client.GetDatabaseInfo(ctx, dbID)\nif err != nil && strings.Contains(err.Error(), \"unmarshal data_sources:\") {\n    // fall back: query without data_source resolution, or refresh API version\n    log.Printf(\"data_sources field missing/malformed for %s; check Notion-Version >= 2025-09-03\", dbID)\n}","preventionTips":["Use Notion API version 2025-09-03+ so databases expose data_sources","Handle the empty-DataSourceID case (client.go:210) as a legitimate outcome","Keep anonymous decode structs aligned with the pinned API version","Test against live API fixtures, not hand-written mocks, after version bumps"],"tags":["notion","json","unmarshal","data-source","api-version"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}