Skip to content

Commit 40658ca

Browse files
author
Tom Ghyselinck
committed
Quick and dirty fix to process enum values from string (json) input.
1 parent d338303 commit 40658ca

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,23 @@ public String getTypeString(Schema p, String prefix, String suffix) {
501501
if (suffix == ")") {
502502
typeSuffix = "," + suffix;
503503
}
504+
505+
// XXX - (1) Q&D fix to make sure that 'enum' string/integer/... type aliases are properly converted:
506+
//
507+
// FIXME - Introducing this poses a warning during codegen:
508+
// WARN o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: <some_type>.
509+
// To fully utilize allOf, please use $ref instead of inline schema definition
510+
//
511+
// It seems to work though...
512+
//
513+
Schema referencedSchema = ModelUtils.getReferencedSchema(this.openAPI, p);
514+
if (ModelUtils.isEnum(referencedSchema)) {
515+
LOGGER.info("Referenced schema '" + getSimpleTypeDeclaration(p) + "' is an enum of type '" + referencedSchema.getType() + "' => '" + getSimpleTypeDeclaration(referencedSchema) + "' (" + referencedSchema.getEnum() + "). Adding referenced type.");
516+
typeSuffix = ", " + getSimpleTypeDeclaration(referencedSchema) + suffix;
517+
} else {
518+
LOGGER.debug("Referenced schema '" + getSimpleTypeDeclaration(p) + "' is not an enum (type '" + referencedSchema.getType() + "' => '" + getSimpleTypeDeclaration(referencedSchema) + "')");
519+
}
520+
504521
if (ModelUtils.isNullable(p)) {
505522
typeSuffix = ", none_type" + suffix;
506523
}
@@ -519,6 +536,23 @@ public String getTypeString(Schema p, String prefix, String suffix) {
519536
return prefix + "[" + getTypeString(inner, "(", ")") + "]" + typeSuffix;
520537
}
521538
String baseType = getSimpleTypeDeclaration(p);
539+
540+
// XXX - (2) Alternative Q&D fix for 'enum' conversions:
541+
//
542+
// FIXME - Introducing this poses a warning during codegen:
543+
// WARN o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: <some_type>.
544+
// To fully utilize allOf, please use $ref instead of inline schema definition
545+
//
546+
// It seems to work though...
547+
//
548+
// Schema referencedSchema = ModelUtils.getReferencedSchema(this.openAPI, p);
549+
// if (ModelUtils.isEnum(referencedSchema)) {
550+
// LOGGER.info("Referenced schema '" + baseType + "' is an enum of type '" + referencedSchema.getType() + "' => '" + getSimpleTypeDeclaration(referencedSchema) + "' (" + referencedSchema.getEnum() + "). Setting baseType as its referenced type.");
551+
// baseType = getSimpleTypeDeclaration(referencedSchema);
552+
// } else {
553+
// LOGGER.debug("Referenced schema '" + baseType + "' is not an enum (type '" + referencedSchema.getType() + "' => '" + getSimpleTypeDeclaration(referencedSchema) + "')");
554+
// }
555+
522556
if (baseType == "file") {
523557
baseType = "file_type";
524558
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,4 +946,16 @@ public static boolean isNullable(Schema schema) {
946946

947947
return false;
948948
}
949+
950+
public static boolean isEnum(Schema schema) {
951+
if (schema == null) {
952+
return false;
953+
}
954+
955+
if (schema.getEnum() != null && !schema.getEnum().isEmpty()) {
956+
return true;
957+
}
958+
959+
return false;
960+
}
949961
}

0 commit comments

Comments
 (0)