Fixes CodegenMediaType schema baseName (#11030)

This commit is contained in:
Justin Black 2021-12-03 10:21:36 -08:00 committed by GitHub
parent b915ad99a8
commit 836e40f1d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -6558,6 +6558,10 @@ public class DefaultCodegen implements CodegenConfig {
codegenParameter.pattern = toRegularExpression(schema.getPattern());
}
protected String toMediaTypeSchemaName(String contentType) {
return toModelName(contentType + "Schema");
}
protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Set<String> imports) {
if (content == null) {
return null;
@ -6605,9 +6609,9 @@ public class DefaultCodegen implements CodegenConfig {
ceMap.put(propName, ce);
}
}
CodegenProperty schemaProp = fromProperty("schema", mt.getSchema());
CodegenMediaType codegenMt = new CodegenMediaType(schemaProp, ceMap);
String contentType = contentEntry.getKey();
CodegenProperty schemaProp = fromProperty(toMediaTypeSchemaName(contentType), mt.getSchema());
CodegenMediaType codegenMt = new CodegenMediaType(schemaProp, ceMap);
cmtContent.put(contentType, codegenMt);
}
return cmtContent;

View File

@ -3922,11 +3922,13 @@ public class DefaultCodegenTest {
CodegenMediaType mt = content.get("application/json");
assertNull(mt.getEncoding());
CodegenProperty cp = mt.getSchema();
assertEquals(cp.baseName, "ApplicationJsonSchema");
assertNotNull(cp);
mt = content.get("text/plain");
assertNull(mt.getEncoding());
cp = mt.getSchema();
assertEquals(cp.baseName, "TextPlainSchema");
assertNotNull(cp);
// Note: the inline model resolver has a bug for this use case; it extracts an inline request body into a component
// but the schema it references is not string type
@ -3940,11 +3942,13 @@ public class DefaultCodegenTest {
mt = content.get("application/json");
assertNull(mt.getEncoding());
cp = mt.getSchema();
assertEquals(cp.baseName, "ApplicationJsonSchema");
assertEquals(cp.complexType, "coordinates");
mt = content.get("text/plain");
assertNull(mt.getEncoding());
cp = mt.getSchema();
assertEquals(cp.baseName, "TextPlainSchema");
assertTrue(cp.isString);
}
}