fern-api / fern

Input OpenAPI. Output SDKs and Docs.
https://buildwithfern.com
Apache License 2.0
2.69k stars 151 forks source link

[Bug] Timestamp type causes "expected ';', found '{'" syntax error in generated types #5189

Open csm-kb opened 5 days ago

csm-kb commented 5 days ago

CLI Version

0.45.0-rc44 - the absolute most bleeding-edge version of the CLI

Generator Version

0.23.5 - observed in fern-go-fiber and fern-go-model

Describe the Bug

When defining an API spec with a timestamp type on an undiscriminated union, I observed the latest version of the generator for fern-go-fiber (and fern-go-model in testing) acting up on the Go source code it generated:

> fern generate
# (snip deprecations)
[api]: ✓ All checks passed
┌─
[api]: fernapi/fern-go-fiber failed to parse Go code: logs\types.go:676:55: expected ';', found '{' (and 3 more errors)  
[api]: fernapi/fern-openapi Downloaded to <>\models\openapi
┌─
│ ✓  fernapi/fern-openapi
│ x  fernapi/fern-go-fiber
└─

Additional Context

After doing some magic to get local development working again, I did a deep dive:

676: if l.typ == "Timestamp" || l.Timestamp != time.Time{} { ^ 677: return json.Marshal(l.Timestamp) 678: } 679: return nil, fmt.Errorf("type %T does not include a non-empty union type", l) 680: }



- Aha, that's a golang parser no-no!
- Found the generator source that controls this:
![image](https://github.com/user-attachments/assets/09e0b00a-39f7-4f56-a7c9-10fcfd9bde5d)
- Looks like it's in `VisitUndiscriminatedUnion`...
- To be safe, I wrap each full if statement in parentheses to resolve -> and a subsequent local build + execution shows successful generation.

I made the needed changes to fix this on a fork, will make a PR shortly stemming from this issue! I will leave the improved error / generated source logging changes as a separate commit to let them be cherry-picked out.