Skip to content

Commit

Permalink
Don't generate null as example value for Python code m(
Browse files Browse the repository at this point in the history
  • Loading branch information
truh committed Jul 15, 2022
1 parent b8d814d commit 489c4a3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>vidapi-generator</artifactId>
<packaging>jar</packaging>
<name>vidapi-generator</name>
<version>1.0.1</version>
<version>1.1.0</version>
<build>
<plugins>
<plugin>
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/io/moonvision/codegen/PythonVidapiGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,14 @@ public String toExampleValue(Schema schema) {
return toExampleValueRecursive(schema, new ArrayList<String>(), 5);
}

private static String exampleNullGuard(String example) {
if ("null".equals(example)) {
return "None";
} else {
return example;
}
}

private String toExampleValueRecursive(Schema schema, List<String> included_schemas, int indentation) {
String indentation_string = "";
for (int i = 0; i < indentation; i++) indentation_string += " ";
Expand All @@ -705,7 +713,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
if (ModelUtils.isStringSchema(schema)) {
example = "'" + example + "'";
}
return example;
return exampleNullGuard(example);
}

if (schema.getEnum() != null && !schema.getEnum().isEmpty()) {
Expand All @@ -716,8 +724,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
}
if (null == example)
LOGGER.warn("Empty enum. Cannot built an example!");

return example;
return exampleNullGuard(example);
} else if (null != schema.get$ref()) {
// $ref case:
Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);
Expand Down Expand Up @@ -871,8 +878,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
if (ModelUtils.isStringSchema(schema)) {
example = "'" + escapeText(example) + "'";
}

return example;
return exampleNullGuard(example);
}

@Override
Expand Down Expand Up @@ -1002,4 +1008,5 @@ public void postProcessFile(File file, String fileType) {
}
}
}

}

0 comments on commit 489c4a3

Please sign in to comment.