Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): minor readability improvements of SwaggerSchemaService #1009

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ public SchemaObject extractSchema(SchemaObject headers) {
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
headerSchema.setProperties(properties);

postProcessSchema(
new HashMap<>(Map.of(schemaName, headerSchema)),
new HashMap<>(Map.of(schemaName, headerSchema)),
DEFAULT_CONTENT_TYPE);
Map<String, Schema> newSchemasToProcess = Map.of(schemaName, headerSchema);
postProcessSchemas(newSchemasToProcess, new HashMap<>(newSchemasToProcess), DEFAULT_CONTENT_TYPE);

return swaggerSchemaUtil.mapSchema(headerSchema);
}
Expand All @@ -90,15 +88,16 @@ public ExtractedSchemas resolveSchema(Type type, String contentType) {
PrimitiveType.fromType(String.class).createProperty());
return new ExtractedSchemas(ComponentSchema.of(payloadSchema), Map.of());
} else {
Map<String, Schema> preProcessSchemas = new LinkedHashMap<>(resolvedSchema.referencedSchemas);
Schema payloadSchema = resolvedSchema.schema;
preProcessSchemas.putIfAbsent(getNameFromType(type), payloadSchema);
preProcessSchemas(payloadSchema, preProcessSchemas, type);
HashMap<String, Schema> postProcessSchemas = new HashMap<>(preProcessSchemas);
postProcessSchema(preProcessSchemas, postProcessSchemas, actualContentType);
Map<String, Schema> newSchemasToProcess = new LinkedHashMap<>(resolvedSchema.referencedSchemas);
newSchemasToProcess.putIfAbsent(getNameFromType(type), resolvedSchema.schema);

preProcessSchemas(resolvedSchema.schema, newSchemasToProcess, type);
HashMap<String, Schema> processedSchemas = new HashMap<>(newSchemasToProcess);
postProcessSchemas(newSchemasToProcess, processedSchemas, actualContentType);

return new ExtractedSchemas(
swaggerSchemaUtil.mapSchemaOrRef(payloadSchema),
swaggerSchemaUtil.mapSchemasMap(postProcessSchemas));
swaggerSchemaUtil.mapSchemaOrRef(resolvedSchema.schema),
swaggerSchemaUtil.mapSchemasMap(processedSchemas));
}
}

Expand Down Expand Up @@ -198,14 +197,14 @@ public String getSimpleNameFromType(Type type) {
return name;
}

private void postProcessSchema(
Map<String, Schema> preProcess, Map<String, Schema> postProcess, String contentType) {
boolean schemasHadEntries = !postProcess.isEmpty();
for (Schema schema : preProcess.values()) {
private void postProcessSchemas(
Map<String, Schema> schemasToProcess, Map<String, Schema> schemas, String contentType) {
boolean schemasHadEntries = !schemas.isEmpty();
for (Schema schema : schemasToProcess.values()) {
for (SchemasPostProcessor processor : schemaPostProcessors) {
processor.process(schema, postProcess, contentType);
processor.process(schema, schemas, contentType);

if (schemasHadEntries && !postProcess.containsValue(schema)) {
if (schemasHadEntries && !schemas.containsValue(schema)) {
// If the post-processor removed the schema, we can stop processing
break;
}
Expand Down
Loading