Skip to content

Commit

Permalink
Merge pull request #15 from Dakskihedron/support-wildcard-media-subtype
Browse files Browse the repository at this point in the history
feat: support responses with wildcard media subtype
  • Loading branch information
karlvr authored Apr 5, 2024
2 parents 18861bc + 13dd058 commit dc97b8c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-laws-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openapi-generator-plus/typescript-express-passport-server-generator": minor
---

Support responses with wildcard media subtype.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodegenGeneratorConstructor, CodegenGeneratorType, CodegenOperation, isCodegenEnumSchema, isCodegenObjectSchema, isCodegenAnyOfSchema, isCodegenInterfaceSchema, isCodegenOneOfSchema, CodegenSchemaType } from '@openapi-generator-plus/types'
import { CodegenGeneratorConstructor, CodegenGeneratorType, CodegenOperation, isCodegenEnumSchema, isCodegenObjectSchema, isCodegenAnyOfSchema, isCodegenInterfaceSchema, isCodegenOneOfSchema, CodegenSchemaType, CodegenMediaType, CodegenContent } from '@openapi-generator-plus/types'
import path from 'path'
import { loadTemplates, emit } from '@openapi-generator-plus/handlebars-templates'
import typescriptGenerator, { options as typescriptCommonOptions, TypeScriptGeneratorContext, chainTypeScriptGeneratorContext, DateApproach } from '@openapi-generator-plus/typescript-generator-common'
Expand Down Expand Up @@ -32,6 +32,14 @@ const createGenerator: CodegenGeneratorConstructor = (config, context) => {
return value.replace(/{(.*?)}/g, ':$1')
})

hbs.registerHelper('mediaType', function(value: CodegenMediaType): string {
return `\`${value.mediaType.replace(/\*/g, '${string}')}\``
})

hbs.registerHelper('isJson', function(value: CodegenContent): boolean {
return !!value.mediaType.mimeType.match('\\bjson$')
})

const relativeSourceOutputPath = generatorOptions.relativeSourceOutputPath
for (const group of doc.groups) {
const operations = group.operations
Expand Down
10 changes: 9 additions & 1 deletion templates/api.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ export default function(app: Express, impl: t.{{className name}}Api) {
{{#if defaultContent.schema}}
let body: any
try {
{{#if (isJson defaultContent)}}
body = {{>frag/toJson defaultContent.schema prefix='v.'}}('response', response.body)
{{else}}
body = response.body
{{/if}}
} catch (error) {
console.error('Invalid response body in {{identifier ../../name}}.{{identifier ../name}}', error)
res.status(500)
Expand All @@ -77,7 +81,11 @@ export default function(app: Express, impl: t.{{className name}}Api) {
{{/each}}
{{/if}}
{{#if defaultContent}}
res.set('Content-Type', {{{stringLiteral defaultContent.mediaType.mediaType}}})
{{#if defaultContent.mediaType.wildcard}}
res.type(response.mediaType)
{{else}}
res.type({{{stringLiteral defaultContent.mediaType.mediaType}}})
{{/if}}
{{/if}}
{{#if defaultContent.schema}}
res.send(body)
Expand Down
3 changes: 3 additions & 0 deletions templates/apiTypes.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export interface {{className (concat ../name '_' code)}}Response {
status: '{{code}}'
actualStatus: number
{{/unless}}
{{#if defaultContent.mediaType.wildcard}}
mediaType: {{{mediaType defaultContent.mediaType}}}
{{/if}}
{{#if defaultContent.nativeType}}
body: {{{defaultContent.nativeType}}}
{{else}}
Expand Down

0 comments on commit dc97b8c

Please sign in to comment.