how to allow "any" body (map[string]interface{})?
With OpenAPI when I define component like this:
Obj1:
type: object
description: "Any valid JSON payload to be passed through for processing"
additionalProperties: true
and generate types (using this generator) I will get following type:
type Obj1 map[string]interface{}
but when I do the same in my AsyncAPI schema I get Error: json: cannot unmarshal bool into Go struct field Schema.components.schemas.additionalProperties of type asyncapiv3.Schema
and when I change it to:
additionalProperties:
type: object
I get something like this:
type WebhookEventMessagePayload struct {
Body BodyPropertyFromWebhookEventMessagePayload `json:"body" validate:"required"`
}
type BodyPropertyFromWebhookEventMessagePayload struct {
AdditionalProperties map[string]AdditionalPropertiesFromBodyPropertyFromWebhookEventMessagePayload `json:"-"`
}
type AdditionalPropertiesFromBodyPropertyFromWebhookEventMessagePayload struct{}
and functions MarshalJSON and UnmarshalJSON but all that looks super weird.
AFAICT AdditionalPropertiesFromBodyPropertyFromWebhookEventMessagePayload is an empty struct, which means it’s not set up to hold any actual data.
I'd need to modify generated code in a following way:
type AdditionalPropertiesFromBodyPropertyFromWebhookEventMessagePayload struct {
Raw json.RawMessage `json:"-"`
}
or comment half of generated code and place simply something like this:
type WebhookEventMessage struct {
Payload struct {
Body map[string]interface{} `json:"body" validate:"required"`
} `json:"payload"`
}
So my question is: am I wrong? How should I allow "any" body in message?
Oh, I've found some workaround for this issue. I added x-go-type flag like this:
body:
type: object
description: "Any valid JSON payload to be passed through for processing"
x-go-type: json.RawMessage
and also removed additionalProperties section. That generates much better code:
type WebhookEventMessagePayload struct {
// Description: Any valid JSON payload to be passed through for processing
Body json.RawMessage `json:"body" validate:"required"`
}
Only leftover is that it still generates following code even if it's no longer used:
// BodyPropertyFromWebhookEventMessagePayload is a schema from the AsyncAPI specification required in messages
// Description: Any valid JSON payload to be passed through for processing
type BodyPropertyFromWebhookEventMessagePayload struct{}
@lerenn I bumped into a similar issue. I believe the problem is that AdditionalProperties has type *Schema (here and here), but it seems that true and false should also be allowed.
Hello @kszafran @mkyc, thanks for taking the time to fill in an issue, and sorry for the very late response (it's been a difficult end of year).
I'll try to find the time to fix this in the next days, but feel free to propose a PR if you feel to do so :)