Skip to content

Commit

Permalink
Added support for freeform types (#18722)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronp authored May 23, 2024
1 parent 638af0f commit 3a23261
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public ScalaCaskServerCodegen() {
// mapped to String as a workaround
typeMapping.put("binary", "String");

typeMapping.put("object", "Value");

cliOptions.add(new CliOption(CodegenConstants.GROUP_ID, CodegenConstants.GROUP_ID_DESC));
cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_ID, CodegenConstants.ARTIFACT_ID_DESC));
cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_VERSION, CodegenConstants.ARTIFACT_VERSION_DESC));
Expand All @@ -129,10 +131,27 @@ public String toDefaultValue(Schema p) {
if (ModelUtils.isMapSchema(p)) {
String inner = getSchemaType(ModelUtils.getAdditionalProperties(p));
return "Map[String, " + inner + "]() ";
} else if (ModelUtils.isFreeFormObject(p)) {
// We're opinionated in this template to use ujson
return "ujson.Null";
}
return super.toDefaultValue(p);
}

@Override
public String getSchemaType(Schema p) {
// pants ... this is also used in the imports, so
// we're getting stuff like this:
//
// import docstore.model.ujson.Value
//
if (ModelUtils.isFreeFormObject(p)) {
// We're opinionated in this template to use ujson
return "Value";
}
return super.getSchemaType(p);
}

@Override
public String testPackage() {
return "jvm/src/test/scala";
Expand Down Expand Up @@ -222,6 +241,7 @@ public void processOpts() {
importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
importMapping.put("LocalTime", "java.time.LocalTime");
importMapping.put("Value", "ujson.Value");
}

static boolean consumesMimetype(CodegenOperation op, String mimetype) {
Expand Down Expand Up @@ -787,6 +807,37 @@ private static String defaultValueNonOption(CodegenProperty p) {
return p.defaultValue;
}


@Override
public CodegenProperty fromProperty(String name, Schema schema) {
CodegenProperty property = super.fromProperty(name, schema);

// Customize type for freeform objects
if (ModelUtils.isFreeFormObject(schema)) {
property.dataType = "Value";
property.baseType = "Value";
}

return property;
}

@Override
public String getTypeDeclaration(Schema schema) {
if (ModelUtils.isFreeFormObject(schema)) {
return "Value";
}
return super.getTypeDeclaration(schema);
}

@Override
public String toModelImport(String name) {
final String result = super.toModelImport(name);
if (importMapping.containsKey(name)) {
return importMapping.get(name);
}
return result;
}

private static String queryArgs(final CodegenOperation op) {
final List<String> list = op.queryParams.stream().map(p -> p.paramName).collect(Collectors.toList());
final String prefix = list.isEmpty() ? "" : ", ";
Expand Down

0 comments on commit 3a23261

Please sign in to comment.