Skip to content
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

Added support for freeform types #18722

Merged
merged 1 commit into from
May 23, 2024
Merged
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
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
Loading