forked from loafle/openapi-generator-original
parent
47f07158a3
commit
ecbd164ed7
@ -21,6 +21,8 @@ import com.samskivert.mustache.Escapers;
|
||||
import com.samskivert.mustache.Mustache;
|
||||
import io.swagger.v3.oas.models.media.ArraySchema;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.media.StringSchema;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.openapitools.codegen.*;
|
||||
@ -354,16 +356,21 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen {
|
||||
|
||||
@Override
|
||||
public String getTypeDeclaration(Schema p) {
|
||||
if (ModelUtils.isArraySchema(p)) {
|
||||
ArraySchema ap = (ArraySchema) p;
|
||||
Schema inner = ap.getItems();
|
||||
return getSchemaType(p) + "[" + getTypeDeclaration(inner) + "]";
|
||||
} else if (ModelUtils.isMapSchema(p)) {
|
||||
Schema inner = getAdditionalProperties(p);
|
||||
|
||||
return getSchemaType(p) + "[String, " + getTypeDeclaration(inner) + "]";
|
||||
Schema<?> schema = ModelUtils.unaliasSchema(this.openAPI, p, importMapping);
|
||||
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
|
||||
if (ModelUtils.isArraySchema(target)) {
|
||||
Schema<?> items = getSchemaItems((ArraySchema) schema);
|
||||
return getSchemaType(target) + "[" + getTypeDeclaration(items) + "]";
|
||||
} else if (ModelUtils.isMapSchema(target)) {
|
||||
Schema<?> inner = getAdditionalProperties(target);
|
||||
if (inner == null) {
|
||||
LOGGER.error("`{}` (map property) does not have a proper inner type defined. Default to type:string", p.getName());
|
||||
inner = new StringSchema().description("TODO default missing map inner type to string");
|
||||
p.setAdditionalProperties(inner);
|
||||
}
|
||||
return super.getTypeDeclaration(p);
|
||||
return getSchemaType(target) + "[String, " + getTypeDeclaration(inner) + "]";
|
||||
}
|
||||
return super.getTypeDeclaration(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,9 +1,16 @@
|
||||
package org.openapitools.codegen.scala;
|
||||
|
||||
import io.swagger.v3.oas.models.Components;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.media.ArraySchema;
|
||||
import io.swagger.v3.oas.models.media.MapSchema;
|
||||
import io.swagger.v3.oas.models.media.ObjectSchema;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.media.StringSchema;
|
||||
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.languages.AbstractScalaCodegen;
|
||||
import org.openapitools.codegen.utils.ModelUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@ -99,19 +106,28 @@ public class AbstractScalaCodegenTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkScalaTypeDeclaration() {
|
||||
|
||||
final AbstractScalaCodegen codegen = new P_AbstractScalaCodegen();
|
||||
|
||||
void checkTypeDeclarationWithByteString() {
|
||||
Schema<?> byteArraySchema = new ObjectSchema();
|
||||
byteArraySchema.setType("string");
|
||||
byteArraySchema.setFormat("byte");
|
||||
byteArraySchema.setDescription("Schema with byte string");
|
||||
|
||||
Assert.assertEquals(codegen.getTypeDeclaration(byteArraySchema), "Array[Byte]",
|
||||
Assert.assertEquals(fakeScalaCodegen.getTypeDeclaration(byteArraySchema), "Array[Byte]",
|
||||
"OpenApi File type represented as byte string should be represented as Array[Byte] scala type");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkTypeDeclarationWithStringToArrayModelMapping() {
|
||||
// Create an alias to an array schema
|
||||
final Schema<?> nestedArraySchema = new ArraySchema().items(new StringSchema());
|
||||
// Create a map schema with additionalProperties type set to array alias
|
||||
final Schema<?> mapSchema = new MapSchema().additionalProperties(new Schema().$ref("#/components/schemas/NestedArray"));
|
||||
fakeScalaCodegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("NestedArray", nestedArraySchema)));
|
||||
|
||||
ModelUtils.setGenerateAliasAsModel(false);
|
||||
Assert.assertEquals(fakeScalaCodegen.getTypeDeclaration(mapSchema), "Map[String, List[String]]");
|
||||
|
||||
ModelUtils.setGenerateAliasAsModel(true);
|
||||
Assert.assertEquals(fakeScalaCodegen.getTypeDeclaration(mapSchema), "Map[String, NestedArray]");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user