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

how to allow "any" body (map[string]interface{})? #261

Open
mkyc opened this issue Sep 25, 2024 · 3 comments
Open

how to allow "any" body (map[string]interface{})? #261

mkyc opened this issue Sep 25, 2024 · 3 comments

Comments

@mkyc
Copy link

mkyc commented Sep 25, 2024

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?

@mkyc
Copy link
Author

mkyc commented Sep 25, 2024

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{}

@kszafran
Copy link

kszafran commented Oct 3, 2024

@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.

@lerenn
Copy link
Owner

lerenn commented Jan 20, 2025

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 :)

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

No branches or pull requests

3 participants