Skip to content

Commit 7b6ac07

Browse files
author
andrewwilsonnew
committed
pushing mediaTypes
1 parent 7622409 commit 7b6ac07

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinMiskServerCodegen.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
import java.io.File;
2222
import java.util.EnumSet;
23+
import java.util.HashMap;
2324
import java.util.List;
2425
import java.util.Locale;
26+
import java.util.Map;
2527

2628
import static org.openapitools.codegen.utils.StringUtils.camelize;
2729

@@ -111,11 +113,38 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
111113
List<CodegenOperation> operations = objectMap.getOperation();
112114

113115
for (CodegenOperation operation : operations) {
116+
117+
if (operation.hasConsumes) {
118+
for (Map<String, String> consumes : operation.consumes) {
119+
consumes.computeIfPresent("mediaType", (__, mediaType) -> mapMediaType(mediaType));
120+
}
121+
}
122+
123+
if (operation.hasProduces) {
124+
for (Map<String, String> produces : operation.produces) {
125+
produces.computeIfPresent("mediaType", (__, mediaType) -> mapMediaType(mediaType));
126+
}
127+
}
128+
114129
// http method verb conversion (e.g. PUT => Put)
115130
operation.httpMethod = camelize(operation.httpMethod.toLowerCase(Locale.ROOT));
116131
}
117132
return objs;
118133
}
134+
135+
private String mapMediaType(String mediaType) {
136+
return MEDIA_MAPPING.get(mediaType);
137+
}
138+
139+
private final static Map<String, String> MEDIA_MAPPING = getMappings();
140+
141+
private static Map<String, String> getMappings() {
142+
Map<String, String> result = new HashMap<>();
143+
// @todo add others as needed
144+
result.put("application/json", "MediaTypes.APPLICATION_JSON");
145+
result.put("application/xml", "MediaTypes.APPLICATION_XML");
146+
return result;
147+
}
119148
}
120149

121150

modules/openapi-generator/src/main/resources/kotlin-misk/apiController.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ import misk.web.mediatype.MediaTypes
9090
value = [{{#responses}}ApiResponse(code = {{{code}}}, message = "{{{message}}}"{{#baseType}}, response = {{{.}}}::class{{/baseType}}{{#containerType}}, responseContainer = "{{{.}}}"{{/containerType}}){{^-last}},{{/-last}}{{/responses}}]){{/swagger1AnnotationLibrary}}
9191
@{{httpMethod}}("{{path}}")
9292
@Description("""{{{unescapedNotes}}}""")
93-
@RequestContentType({{#hasConsumes}}{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}{{/hasConsumes}})
94-
@ResponseContentType({{#hasProduces}}{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}{{/hasProduces}})
93+
@RequestContentType({{#hasConsumes}}{{#consumes}}{{{mediaType}}}{{^-last}}, {{/-last}}{{/consumes}}{{/hasConsumes}})
94+
@ResponseContentType({{#hasProduces}}{{#produces}}{{{mediaType}}}{{^-last}}, {{/-last}}{{/produces}}{{/hasProduces}})
9595
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
9696
@Suppress("unused")
9797
@RequestMapping(

0 commit comments

Comments
 (0)