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

Need a plugin that help wrap upload method and generate right documents for openapi #78

Open
RyoJerryYu opened this issue Jan 10, 2025 · 1 comment

Comments

@RyoJerryYu
Copy link
Owner

in openapi json it should be:

      "post": {
        "summary": "",
        "description": "",
        "operationId": "BizComfySvc_ComfyUploadImage",
        "responses": {
          "200": {
            "description": "A successful response.",
            "schema": {
              "$ref": "#/definitions/biz_comfyComfyUploadImageResp"
            }
          },
          "default": {
            "description": "An unexpected error response.",
            "schema": {
              "$ref": "#/definitions/googlerpcStatus"
            }
          }
        },
        "parameters": [
          {
            "name": "image",
            "description": "",
            "in": "body",
            "type": "file",
            "required": true
          }

in protobuf method it may be:

  rpc ComfyUploadImage(ComfyUploadImageReq) returns (ComfyUploadImageResp) {
    option (google.api.http) = {
      post: "/api/v1/comfy/upload/image"
      body: "image"
    };
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      consumes: "multipart/form-data"
      produces: "application/json"
    };
  }

in response it maybe:

message ComfyUploadImageReq {
  int64 app_id = 1; // App ID
  string client_id = 2; // Client ID
  google.api.HttpBody image = 3 [(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
    format: "binary"
    type: STRING
  }];
}
message ComfyUploadImageResp {
  string name = 1;
  string sub_folder = 2;
  string type = 3;
}

What's difficult:

  1. parameter could not be in formData with google.api.http
  2. type could not be file
@RyoJerryYu
Copy link
Owner Author

For server side marshaling, add:

		runtime.WithMarshalerOption("multipart/form-data", &grpcconn.UploadHTTPBodyMarshaler{
			Marshaler: detaultMarshaler,
		}),

and

type UploadHTTPBodyMarshaler struct {
	runtime.Marshaler
}

// Unmarshal unmarshals "data" into "v".
// "v" must be a pointer value.
func (h *UploadHTTPBodyMarshaler) Unmarshal(data []byte, v interface{}) error {
	if httpBody, ok := v.(*httpbody.HttpBody); ok {
		httpBody.Data = data
		return nil
	}
	return h.Marshaler.Unmarshal(data, v)
}

// NewDecoder returns a Decoder which reads byte sequence from "r".
func (h *UploadHTTPBodyMarshaler) NewDecoder(r io.Reader) runtime.Decoder {
	return runtime.DecoderFunc(func(v interface{}) error {
		if httpBody, ok := v.(**httpbody.HttpBody); ok {
			buffer, err := io.ReadAll(r)
			if err != nil {
				return err
			}
			*httpBody = new(httpbody.HttpBody)
			(*httpBody).Data = buffer
			(*httpBody).ContentType = "multipart/form-data"
			return nil
		}
		return h.Marshaler.NewDecoder(r).Decode(v)
	})
}

can well get the binary

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

1 participant