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

RFC: [go] define "any-type" schema as an empty interface in generated go code. #888

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -83,7 +83,9 @@ public AbstractGoCodegen() {
"complex64",
"complex128",
"rune",
"byte")
"byte",
"interface{}"
)
);

instantiationTypes.clear();
Expand Down Expand Up @@ -298,12 +300,19 @@ public String getSchemaType(Schema p) {

if (ref != null && !ref.isEmpty()) {
type = openAPIType;
} else if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
if (languageSpecificPrimitives.contains(type))
return (type);
} else
type = openAPIType;
} else {
// Handle "any type" as an empty interface
if (openAPIType == "object" && p != null && !ModelUtils.isObjectSchema(p) && !ModelUtils.isMapSchema(p)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashanbrown Please use "object".equals(openAPIType) instead

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashanbrown can you please fix it? Here in Arduino we really appreciate to have this PR merged :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure @eclipse1985, I've made that change. It wasn't sure that the team was really offering to merge this though because I didn't see further comments from @antihax, @bvwells or @grokify about this or my follow-up comment at #888 (comment). I'd still be happy to have this merged as is but my impression was there might be a larger issue here.

return "interface{}";
}

if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
if (languageSpecificPrimitives.contains(type))
return (type);
} else
type = openAPIType;
}
return type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ public void simpleModelTest() {
.addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT))
.addProperties("name", new StringSchema())
.addProperties("createdAt", new DateTimeSchema())
.addProperties("anyValue", new Schema())
.addRequiredItem("id")
.addRequiredItem("name");
.addRequiredItem("name")
.addRequiredItem("anyValue");
final DefaultCodegen codegen = new GoClientCodegen();
final CodegenModel cm = codegen.fromModel("sample", model, Collections.singletonMap("sample", model));

Assert.assertEquals(cm.name, "sample");
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.description, "a sample model");
Assert.assertEquals(cm.vars.size(), 3);
Assert.assertEquals(cm.vars.size(), 4);
Assert.assertEquals(cm.imports.size(), 1);

final CodegenProperty property1 = cm.vars.get(0);
Expand Down Expand Up @@ -85,10 +87,21 @@ public void simpleModelTest() {
Assert.assertEquals(property3.name, "CreatedAt");
Assert.assertNull(property3.defaultValue);
Assert.assertEquals(property3.baseType, "time.Time");
Assert.assertFalse(property3.hasMore);
Assert.assertTrue(property3.hasMore);
Assert.assertFalse(property3.required);

final CodegenProperty property4 = cm.vars.get(3);
Assert.assertEquals(property4.baseName, "anyValue");
Assert.assertEquals(property4.baseType, "interface{}");
Assert.assertEquals(property4.dataType, "interface{}");
Assert.assertTrue(property1.isPrimitiveType);
Assert.assertEquals(property4.name, "AnyValue");
Assert.assertNull(property4.defaultValue);
Assert.assertFalse(property4.hasMore);
Assert.assertTrue(property4.required);
}


@Test(description = "convert a model with list property")
public void listPropertyTest() {
final Schema model = new Schema()
Expand Down