Skip to content
Merged
Show file tree
Hide file tree
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 @@ -563,6 +563,10 @@ private String toExampleValueRecursive(Schema schema, List<Schema> includedSchem
example = "{ }";
}
} else if (ModelUtils.isObjectSchema(schema)) {
if (ModelUtils.isFreeFormObject(schema, this.openAPI)) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
// free-form objects have no generated model, so the example is a plain dict
return "{ }";
}
if (StringUtils.isBlank(schema.getTitle())) {
example = "None";
return example;
Expand All @@ -574,7 +578,7 @@ private String toExampleValueRecursive(Schema schema, List<Schema> includedSchem
toExclude = schema.getDiscriminator().getPropertyName();
}

example = packageName + ".models." + underscore(schema.getTitle()) + "." + schema.getTitle() + "(";
example = packageName + ".models." + toModelFilename(schema.getTitle()) + "." + toModelName(schema.getTitle()) + "(";

// if required only:
// List<String> reqs = schema.getRequired();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,34 @@ public void testExampleValueForSiblingsSharingAModel() {
}
}

@Test(description = "inline object examples use the generated module and class names")
public void testExampleValueForInlineObjects() {
// The inline model resolver names this schema _holder_child; the generated file is
// holder_child.py with class HolderChild, so the example must use those names.
final Schema child = new ObjectSchema().addProperty("prop", new StringSchema());
child.setTitle("_holder_child");
final Schema holder = new ObjectSchema()
.addProperty("child", child)
// a free-form object has no model at all, it is a plain dict
.addProperty("aProperty", new ObjectSchema());
holder.setTitle("Holder");
final OpenAPI openAPI = new OpenAPI().components(new io.swagger.v3.oas.models.Components()
.addSchemas("Holder", holder));
final PythonClientCodegen codegen = new PythonClientCodegen();
codegen.setOpenAPI(openAPI);

final String example = codegen.toExampleValue(holder);

Assert.assertTrue(example.contains("child = openapi_client.models.holder_child.HolderChild("),
"inline model path wrong in:\n" + example);
Assert.assertTrue(example.contains("a_property = { }"),
"free-form object should be a dict in:\n" + example);
Assert.assertFalse(example.contains("models.a_property."), "free-form object got a model path in:\n" + example);

// the same holds for a free-form object that never got a title, e.g. bare array items
Assert.assertEquals(codegen.toExampleValue(new ObjectSchema()), "{ }");
}

@Test(description = "test single quotes escape")
public void testSingleQuotes() {
final PythonClientCodegen codegen = new PythonClientCodegen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> Parent:
return Parent(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> ParentWithOptionalDict:
return ParentWithOptionalDict(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> Parent:
return Parent(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> ParentWithOptionalDict:
return ParentWithOptionalDict(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> Parent:
return Parent(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> ParentWithOptionalDict:
return ParentWithOptionalDict(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> Parent:
return Parent(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> ParentWithOptionalDict:
return ParentWithOptionalDict(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> Parent:
return Parent(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> ParentWithOptionalDict:
return ParentWithOptionalDict(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> Parent:
return Parent(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> ParentWithOptionalDict:
return ParentWithOptionalDict(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> Parent:
return Parent(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_instance(self, include_optional) -> ParentWithOptionalDict:
return ParentWithOptionalDict(
optional_dict = {
'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty(
a_property = petstore_api.models.a_property.aProperty(), )
a_property = { }, )
}
)
else:
Expand Down
Loading