Skip to content

Commit

Permalink
feat(go): Support custom Content-Type
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney committed Sep 26, 2024
1 parent c0dd19b commit 8938563
Show file tree
Hide file tree
Showing 58 changed files with 3,771 additions and 6 deletions.
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
27 changes: 27 additions & 0 deletions seed/go-fiber/go-content-type/.github/workflows/ci.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions seed/go-fiber/go-content-type/.mock/definition/api.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions seed/go-fiber/go-content-type/.mock/definition/imdb.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions seed/go-fiber/go-content-type/.mock/fern.config.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions seed/go-fiber/go-content-type/.mock/generators.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 141 additions & 0 deletions seed/go-fiber/go-content-type/core/extra_properties.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8938563

Please sign in to comment.