Skip to content

Commit 19b4657

Browse files
committed
fixed wrong datatype for arrays with composed schema items.
1 parent dae9dc0 commit 19b4657

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/main/java/io/swagger/codegen/v3/generators/go/AbstractGoCodegen.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import io.swagger.codegen.v3.CodegenProperty;
1010
import io.swagger.codegen.v3.ISchemaHandler;
1111
import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
12+
import io.swagger.codegen.v3.generators.SchemaHandler;
1213
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
1314
import io.swagger.v3.core.util.Yaml;
1415
import io.swagger.v3.oas.models.OpenAPI;
1516
import io.swagger.v3.oas.models.media.ArraySchema;
17+
import io.swagger.v3.oas.models.media.ComposedSchema;
1618
import io.swagger.v3.oas.models.media.MapSchema;
1719
import io.swagger.v3.oas.models.media.Schema;
1820
import org.apache.commons.lang3.StringUtils;
@@ -243,12 +245,32 @@ public String getTypeDeclaration(Schema schema) {
243245
if(schema instanceof ArraySchema) {
244246
ArraySchema arraySchema = (ArraySchema) schema;
245247
Schema inner = arraySchema.getItems();
246-
return "[]" + getTypeDeclaration(inner);
248+
if (inner instanceof ComposedSchema && schema.getExtensions() != null && schema.getExtensions().containsKey("x-schema-name")) {
249+
final ComposedSchema composedSchema = (ComposedSchema) inner;
250+
final String prefix;
251+
if (composedSchema.getAllOf() != null && !composedSchema.getAllOf().isEmpty()) {
252+
prefix = SchemaHandler.ALL_OF_PREFFIX;
253+
} else if (composedSchema.getOneOf() != null && !composedSchema.getOneOf().isEmpty()) {
254+
prefix = SchemaHandler.ONE_OF_PREFFIX;
255+
} else {
256+
prefix = SchemaHandler.ANY_OF_PREFFIX;
257+
}
258+
return "[]" + toModelName(prefix + schema.getExtensions().get("x-schema-name")) + SchemaHandler.ARRAY_ITEMS_SUFFIX;
259+
} else {
260+
return "[]" + getTypeDeclaration(inner);
261+
}
247262
} else if (schema instanceof MapSchema && hasSchemaProperties(schema)) {
248263
MapSchema mapSchema = (MapSchema) schema;
249264
Schema inner = (Schema) mapSchema.getAdditionalProperties();
250265

251266
return getSchemaType(schema) + "[string]" + getTypeDeclaration(inner);
267+
} else if (schema.get$ref() != null) {
268+
final String simpleRefName = OpenAPIUtil.getSimpleRef(schema.get$ref());
269+
final Schema refSchema = OpenAPIUtil.getSchemaFromName(simpleRefName, this.openAPI);
270+
if (refSchema != null && (refSchema instanceof ArraySchema || refSchema instanceof MapSchema)) {
271+
refSchema.addExtension("x-schema-name", simpleRefName);
272+
return getTypeDeclaration(refSchema);
273+
}
252274
}
253275
// Not using the supertype invocation, because we want to UpperCamelize
254276
// the type.
@@ -271,7 +293,7 @@ public String getSchemaType(Schema schema) {
271293

272294
if (schema.get$ref() != null) {
273295
final Schema refSchema = OpenAPIUtil.getSchemaFromName(schemaType, this.openAPI);
274-
if (refSchema != null && !isObjectSchema(refSchema)) {
296+
if (refSchema != null && !isObjectSchema(refSchema) && refSchema.getEnum() == null) {
275297
schemaType = super.getSchemaType(refSchema);
276298
}
277299
}

0 commit comments

Comments
 (0)