Skip to content

adjusted negotiation logic so that it is possible to add or overwrite… #4276

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kkoehler
Copy link

… render logic for sepcific content-types and added/referenced a default protobuf renderer.

The following code is a sample which shows the handling for ProtoBuf data. The old API is still working.

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"google.golang.org/protobuf/types/known/structpb"
)

func main() {
	g := gin.Default()

	testData, _ := structpb.NewStruct(map[string]interface{}{
		"key": "value",
	})

	negotiate := gin.NewNegotiate([]string{
		binding.MIMEJSON,
		binding.MIMEPROTOBUF,
	})
	g.GET("/negotiate", func(c *gin.Context) {
		c.Negotiate(200, negotiate.WithData(testData))
	})

	g.Run(":8080")
}

… render logic for sepcific content-types and added/referenced a default protobuf renderer.
@kkoehler
Copy link
Author

resolves: #3799

@appleboy appleboy requested a review from Copilot June 26, 2025 01:55
@appleboy
Copy link
Member

@kkoehler Please add more testing.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the content negotiation logic to use a pluggable map of render functions, enables adding or overriding renderers, and introduces a default Protobuf renderer.

  • Introduce NewNegotiate and fluent WithData for building negotiation configs
  • Replace the hard-coded switch in Context.Negotiate with a lookup on negotiationRenderMappings
  • Add AddNegotiationRenderMapping API and include MIMEPROTOBUF in the default mappings
Comments suppressed due to low confidence (1)

context.go:1278

  • [nitpick] The parameter name binding shadows the imported binding package and may be confusing. Consider renaming it to renderFunc or fn for clarity.
func AddNegotiationRenderMapping(mimeType string, binding NegotiationRenderFunc) {

kkoehler and others added 2 commits June 27, 2025 09:42
Changed from iteration to lookup

Co-authored-by: Copilot <[email protected]>
@kkoehler
Copy link
Author

kkoehler commented Jun 27, 2025

@kkoehler Please add more testing.

@appleboy added some tests to the pull request

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 this pull request may close these issues.

3 participants