Skip to content

Missing time import when using xsd:time with optional fields #115

@kruegge

Description

@kruegge

Description
When generating Go code from XSD schemas that use xsd:time (mapped to time.Time), the time package import is missing from the generated code if the field is optional (pointer type).

Root Cause
The code generator checks for time.Time using exact string equality (fieldType == "time.Time"), but when a field is optional, the type becomes *time.Time (pointer), causing the check to fail and the import to be omitted.

Affected Code
The issue occurs in genGo.go in multiple locations where time.Time detection happens.

Example
XSD:

<xsd:element name="StartTime" type="xsd:time" minOccurs="0" maxOccurs="1"/>

Generated Go (broken):

// Missing: import "time"

type SomeType struct {
    StartTime *time.Time `xml:"StartTime"`  // ❌ Compilation error: undefined: time
}

Expected:

import "time"

type SomeType struct {
    StartTime *time.Time `xml:"StartTime"`  // ✅ Works correctly
}

Solution
Change the exact equality check to a substring check to handle both time.Time and *time.Time:

Before:

if fieldType == "time.Time" {
    gen.ImportTime = true
}

After:

if strings.Contains(fieldType, "time.Time") {
    gen.ImportTime = true
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions