Skip to content

Commit

Permalink
fix: Fix issue when oneOf/anyOf could be array or simple schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
en-milie committed Sep 25, 2024
1 parent b42f446 commit 82fbf6f
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public class OpenAPIModelGenerator {
private final Map<String, Integer> callStackCounter;
private final boolean useDefaults;
private final int arraySize;
private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper customDepthMapper = new ObjectMapper();
private final ObjectMapper simpleObjectMapper = new ObjectMapper();

/**
* Constructs an OpenAPIModelGenerator with the specified configuration.
Expand All @@ -91,9 +92,10 @@ public OpenAPIModelGenerator(CatsGlobalContext catsGlobalContext, ValidDataForma
this.useDefaults = useDefaults;
this.arraySize = arraySize;
SimpleModule module = new SimpleModule();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // Exclude null values
customDepthMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // Exclude null values
module.addSerializer(Object.class, new DepthLimitingSerializer());
mapper.registerModule(module);
customDepthMapper.registerModule(module);
simpleObjectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // Exclude null values
}


Expand All @@ -117,7 +119,7 @@ public Map<String, String> generate(String modelName) {
example = writeObjectAsString(exampleObject);
globalContext.recordError("Generate sample it's too large to be processed in memory. CATS used a limiting depth serializer which might not include all expected fields. Re-run CATS with a smaller --selfReferenceDepth value, like --selfReferenceDepth 2");
}

resetCatsGeneratedExamples();
if (example != null) {
kv.put(EXAMPLE, example);
return Map.copyOf(kv);
Expand All @@ -129,11 +131,17 @@ public Map<String, String> generate(String modelName) {
return Collections.emptyMap();
}

private void resetCatsGeneratedExamples() {
for (Schema<?> schema : catsGeneratedExamples) {
schema.setExample(null);
}
}

private String writeObjectAsString(Object exampleObject) {
try {
StringWriter stringWriter = new StringWriter();
JsonGenerator jsonGenerator = mapper.getFactory().createGenerator(stringWriter);
mapper.writeValue(jsonGenerator, exampleObject);
JsonGenerator jsonGenerator = customDepthMapper.getFactory().createGenerator(stringWriter);
customDepthMapper.writeValue(jsonGenerator, exampleObject);
return stringWriter.toString();
} catch (IOException e) {
logger.debug("Error writing large object as string: {}", e.getMessage());
Expand All @@ -143,7 +151,7 @@ private String writeObjectAsString(Object exampleObject) {

public String tryToSerializeExample(Object obj) {
try {
return Json.pretty().writeValueAsString(obj);
return simpleObjectMapper.writeValueAsString(obj);
} catch (Exception e) {
logger.debug("Generated object is too large. Switching to custom depth aware serializer...");
return null;
Expand Down Expand Up @@ -708,11 +716,7 @@ private void addXXXOfExamples(Map<String, Object> values, Object propertyName, C
for (Schema allOfSchema : allOf) {
String fullSchemaRef = allOfSchema.get$ref();
String schemaRef;

if (CatsModelUtils.isArraySchema(allOfSchema)) {
fullSchemaRef = allOfSchema.getItems().get$ref();
}


Schema schemaToExample = allOfSchema;
if (fullSchemaRef != null) {
schemaRef = CatsModelUtils.getSimpleRef(fullSchemaRef);
Expand Down

0 comments on commit 82fbf6f

Please sign in to comment.