Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't return responses with empty arrays from http ingress verb #1196

Closed
mistermoe opened this issue Apr 8, 2024 · 1 comment · Fixed by #1198
Closed

Can't return responses with empty arrays from http ingress verb #1196

mistermoe opened this issue Apr 8, 2024 · 1 comment · Fixed by #1198
Assignees

Comments

@mistermoe
Copy link
Collaborator

mistermoe commented Apr 8, 2024

Repro

package offerings

import (
	"context"
	"ftl/builtin"

	"github.com/TBD54566975/ftl/go-runtime/ftl"
)

type GetOfferingsResponse struct {
	Data []string `json:"data"`
}

type ApiError struct {
	Error string `json:"error"`
}

//ftl:export
//ftl:ingress GET /offerings
func GetOfferings(ctx context.Context, req builtin.HttpRequest[builtin.Empty]) (builtin.HttpResponse[GetOfferingsResponse, ApiError], error) {
	r := GetOfferingsResponse{
		Data: make([]string, 0),
	}
	return builtin.HttpResponse[GetOfferingsResponse, ApiError]{
		Status: 200,
		Body:   ftl.Some(r),
	}, nil
}
❯ curl -v 'http://localhost:8892/ingress/offerings'
*   Trying [::1]:8892...
* connect to ::1 port 8892 failed: Connection refused
*   Trying 127.0.0.1:8892...
* Connected to localhost (127.0.0.1) port 8892
> GET /ingress/offerings HTTP/1.1
> Host: localhost:8892
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Mon, 08 Apr 2024 00:54:57 GMT
< Content-Length: 2
<
* Connection #0 to host localhost left intact
{}

Expected Output

❯ curl -v 'http://localhost:8892/ingress/offerings'
*   Trying [::1]:8892...
* connect to ::1 port 8892 failed: Connection refused
*   Trying 127.0.0.1:8892...
* Connected to localhost (127.0.0.1) port 8892
> GET /ingress/offerings HTTP/1.1
> Host: localhost:8892
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Mon, 08 Apr 2024 00:54:57 GMT
< Content-Length: 2
<
* Connection #0 to host localhost left intact
{"data": []}

Workaround

switching the response body type to []byte at the cost of losing strong types e.g.

package offerings

import (
	"context"
	"encoding/json"
	"ftl/builtin"

	"github.com/TBD54566975/ftl/go-runtime/ftl"
)

type GetOfferingsResponse struct {
	Data []string `json:"data"`
}

type ApiError struct {
	Error string `json:"error"`
}

//ftl:export
//ftl:ingress GET /offerings
func GetOfferings(ctx context.Context, req builtin.HttpRequest[builtin.Empty]) (builtin.HttpResponse[[]byte, ApiError], error) {
	r := GetOfferingsResponse{
		Data: make([]string, 0),
	}

	b, _ := json.Marshal(r)

	return builtin.HttpResponse[[]byte, ApiError]{
		Status: 200,
		Body:   ftl.Some(b),
	}, nil
}

Other Thoughts

potentially has something to do with how marshaling ftl.Some handles zero values

@github-actions github-actions bot added the triage Issue needs triaging label Apr 8, 2024
@alecthomas alecthomas mentioned this issue Apr 8, 2024
@alecthomas
Copy link
Collaborator

The fields are omitted by the internal encoder here, but the bug is with the ingress marshaler in that it shouldn't be skipping them.

@alecthomas alecthomas self-assigned this Apr 8, 2024
@github-actions github-actions bot removed the triage Issue needs triaging label Apr 8, 2024
alecthomas added a commit that referenced this issue Apr 8, 2024
There should ideally be no relationship between the ingress encoder and
the encoding package, but for now this is the simplest solution.

Fixes #1196
alecthomas added a commit that referenced this issue Apr 8, 2024
There should ideally be no relationship between the ingress encoder and
the encoding package, but for now this is the simplest solution.

Fixes #1196
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants