Skip to content

Commit

Permalink
feat(go): Support custom Content-Type (#4763)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney committed Sep 26, 2024
1 parent 4ce08dd commit 90643d4
Show file tree
Hide file tree
Showing 74 changed files with 5,820 additions and 118 deletions.
3 changes: 3 additions & 0 deletions fern/pages/changelogs/go-sdk/2024-09-26.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.26.0
**`(feat):`** Add support for sending custom Content-Type header values defined in the API.

50 changes: 44 additions & 6 deletions generators/go/internal/generator/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2118,9 +2118,6 @@ func (f *fileWriter) endpointFromIR(
requestType = typeReferenceToGoType(requestBody.TypeReference.RequestBodyType, f.types, scope, f.baseImportPath, "" /* The type is always imported */, false)
case "bytes":
contentType = "application/octet-stream"
if irEndpoint.RequestBody.Bytes.ContentType != nil {
contentType = *irEndpoint.RequestBody.Bytes.ContentType
}
requestType = "[]byte"
requestValueName = "requestBuffer"
requestIsBytes = true
Expand All @@ -2135,9 +2132,6 @@ func (f *fileWriter) endpointFromIR(
requestType = fmt.Sprintf("*%s.%s", scope.AddImport(requestImportPath), irEndpoint.SdkRequest.Shape.Wrapper.WrapperName.PascalCase.UnsafeName)
if irEndpoint.RequestBody != nil && irEndpoint.RequestBody.Bytes != nil {
contentType = "application/octet-stream"
if irEndpoint.RequestBody.Bytes.ContentType != nil {
contentType = *irEndpoint.RequestBody.Bytes.ContentType
}
requestValueName = "requestBuffer"
requestIsBytes = true
requestIsOptional = irEndpoint.RequestBody.Bytes.IsOptional
Expand Down Expand Up @@ -2314,6 +2308,11 @@ func (f *fileWriter) endpointFromIR(
optionConstructor = "core.NewIdempotentRequestOptions(opts...)"
}

contentTypeOverride := contentTypeFromRequestBody(irEndpoint.RequestBody)
if contentTypeOverride != "" {
contentType = contentTypeOverride
}

return &endpoint{
Name: irEndpoint.Name,
Docs: irEndpoint.Docs,
Expand Down Expand Up @@ -3047,6 +3046,45 @@ func formatForValueType(typeReference *ir.TypeReference, types map[ir.TypeId]*ir
}
}

func contentTypeFromRequestBody(
requestBody *ir.HttpRequestBody,
) string {
if requestBody == nil {
return ""
}
visitor := new(contentTypeVisitor)
if err := requestBody.Accept(visitor); err != nil {
return ""
}
if visitor.contentType == nil {
return ""
}
return *visitor.contentType
}

type contentTypeVisitor struct {
contentType *string
}

func (c *contentTypeVisitor) VisitInlinedRequestBody(inlinedRequestBody *ir.InlinedRequestBody) error {
c.contentType = inlinedRequestBody.ContentType
return nil
}

func (c *contentTypeVisitor) VisitReference(reference *ir.HttpRequestBodyReference) error {
c.contentType = reference.ContentType
return nil
}

func (c *contentTypeVisitor) VisitFileUpload(fileUpload *ir.FileUploadRequest) error {
return nil
}

func (c *contentTypeVisitor) VisitBytes(bytes *ir.BytesRequest) error {
c.contentType = bytes.ContentType
return nil
}

func typeReferenceFromJsonResponse(
jsonResponse *ir.JsonResponse,
) *ir.TypeReference {
Expand Down
6 changes: 6 additions & 0 deletions generators/go/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
- version: 0.26.0
changelogEntry:
- type: feat
summary: >-
Add support for sending custom Content-Type header values defined in the API.
irVersion: 40
- version: 0.25.0
changelogEntry:
- type: feat
Expand Down
Loading

0 comments on commit 90643d4

Please sign in to comment.