Skip to content

Fixes #56 ; Don't attempt to set Content-Type header if the request is using multipart-formdata #57

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 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/handlebars-templates/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { promises as fs } from 'fs'
import path from 'path'
import Handlebars, { HelperOptions } from 'handlebars'
import { camelCase, capitalize, pascalCase, uniquePropertiesIncludingInherited, debugStringify, uniquePropertiesIncludingInheritedForParents } from '@openapi-generator-plus/generator-common'
import { CodegenGeneratorContext, CodegenSchemaType, CodegenResponse, CodegenRequestBody, CodegenObjectSchema, CodegenOperation, CodegenVendorExtensions, CodegenExamples, CodegenContent, CodegenLogLevel, CodegenSchemaUsage, CodegenSchema, isCodegenParameter, isCodegenProperty } from '@openapi-generator-plus/types'
import { CodegenGeneratorContext, CodegenSchemaType, CodegenResponse, CodegenRequestBody, CodegenObjectSchema, CodegenOperation, CodegenVendorExtensions, CodegenExamples, CodegenContent, CodegenLogLevel, CodegenSchemaUsage, CodegenSchema, isCodegenParameter, isCodegenProperty, CodegenContentEncodingType } from '@openapi-generator-plus/types'
import { snakeCase, constantCase, sentenceCase, capitalCase } from 'change-case'
import pluralize from 'pluralize'
import * as idx from '@openapi-generator-plus/indexed-type'
Expand Down Expand Up @@ -910,10 +910,10 @@ export function registerStandardHelpers(hbs: typeof Handlebars, { generator, log
return !!value.mediaType.mimeType.match('\\bjson$')
})
hbs.registerHelper('isContentMultipart', function(value: CodegenContent): boolean {
return !!value.mediaType.mimeType.match('^multipart/.*')
return value.encoding?.type === CodegenContentEncodingType.MULTIPART
})
hbs.registerHelper('isContentFormUrlEncoded', function(value: CodegenContent): boolean {
return value.mediaType.mimeType === 'application/x-www-form-urlencoded'
return value.encoding?.type === CodegenContentEncodingType.WWW_FORM_URLENCODED
})

function isEmpty(ob: UnknownObject) {
Expand Down
20 changes: 19 additions & 1 deletion packages/java-jaxrs-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodegenSchemaType, CodegenConfig, CodegenGeneratorContext, CodegenDocument, CodegenGenerator, isCodegenObjectSchema, isCodegenEnumSchema, CodegenNativeType, CodegenProperty, CodegenAllOfStrategy, CodegenAnyOfStrategy, CodegenOneOfStrategy, CodegenLogLevel, isCodegenInterfaceSchema, isCodegenWrapperSchema, CodegenSchema, CodegenSchemaPurpose } from '@openapi-generator-plus/types'
import { CodegenSchemaType, CodegenConfig, CodegenGeneratorContext, CodegenDocument, CodegenGenerator, isCodegenObjectSchema, isCodegenEnumSchema, CodegenNativeType, CodegenProperty, CodegenAllOfStrategy, CodegenAnyOfStrategy, CodegenOneOfStrategy, CodegenLogLevel, isCodegenInterfaceSchema, isCodegenWrapperSchema, CodegenSchema, CodegenSchemaPurpose, CodegenContentEncodingType } from '@openapi-generator-plus/types'
import { CodegenOptionsJava } from './types'
import path from 'path'
import Handlebars from 'handlebars'
Expand Down Expand Up @@ -612,6 +612,24 @@ export default function createGenerator(config: CodegenConfig, context: JavaGene
return 'javax'
}
})

hbs.registerHelper('hasMultipartOperations', function() {
for (const group of doc.groups) {
for (const operation of group.operations) {
if (operation.requestBody?.contents) {
if (operation.requestBody.contents.find(content => content.encoding?.type === CodegenContentEncodingType.MULTIPART)) {
return true
}
}
if (operation.responses) {
if (idx.find(operation.responses, resp => !!resp.contents?.find(content => content.encoding?.type === CodegenContentEncodingType.MULTIPART))) {
return true
}
}
}
}
return false
})

await loadTemplates(path.resolve(__dirname, '..', 'templates'), hbs)
if (context.loadAdditionalTemplates) {
Expand Down
2 changes: 1 addition & 1 deletion packages/java-jaxrs-common/templates/frag/bodyParam.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ required = true
{{#if deprecated}}
deprecated = true
{{/if}}
{{/join}}) {{/if}}{{{nativeType}}} {{identifier name}}
{{/join}}) {{#ifeq defaultContent.encoding.type 'MULTIPART'}}@org.apache.cxf.jaxrs.ext.multipart.Multipart {{/ifeq}}{{/if}}{{{nativeType}}} {{identifier name}}
3 changes: 3 additions & 0 deletions packages/java-jaxrs-server/templates/invoker.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public abstract class Abstract{{invokerName}} extends {{javax}}.ws.rs.core.Appli

protected void addProviders() {
classes.add({{{apiProviderPackage}}}.ApiJaxbJsonProvider.class);
{{#if (hasMultipartOperations)}}
classes.add(org.apache.cxf.jaxrs.provider.MultipartProvider.class);
{{/if}}
}

protected void addEndpoints() {
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript-fetch-client/templates/api.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export const {{className name}}ApiFetchParamCreator = function (configuration?:

{{/if}}
{{#with requestBody}}
{{#unless (isContentMultipart defaultContent)}}
{{#unless consumes}}
localVarHeaderParameter.set('Content-Type', 'application/json');
{{/unless}}
Expand All @@ -134,6 +135,7 @@ export const {{className name}}ApiFetchParamCreator = function (configuration?:
{{/if}}
{{/each}}

{{/unless}}
{{/with}}
localVarRequestOptions.headers = localVarHeaderParameter;
{{#if formParams}}
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript-fetch-client2/templates/api.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const {{className name}}ApiFetchParamCreator = function (configuration?:

{{/if}}
{{#with requestBody}}
{{#unless (isContentMultipart defaultContent)}}
{{#unless consumes}}
localVarHeaderParameter.set('Content-Type', 'application/json');
{{/unless}}
Expand All @@ -100,6 +101,7 @@ export const {{className name}}ApiFetchParamCreator = function (configuration?:
{{/if}}
{{/each}}

{{/uness}}
{{/with}}
localVarRequestOptions.headers = localVarHeaderParameter;
{{#if formParams}}
Expand Down