forked from smallrye/smallrye-open-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for removing unused models
Fixes smallrye#1183 Signed-off-by: Michael Edgar <michael@xlate.io>
- Loading branch information
Showing
9 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
core/src/main/java/io/smallrye/openapi/api/util/UnusedSchemaFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package io.smallrye.openapi.api.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.microprofile.openapi.OASFilter; | ||
import org.eclipse.microprofile.openapi.models.Components; | ||
import org.eclipse.microprofile.openapi.models.OpenAPI; | ||
import org.eclipse.microprofile.openapi.models.media.Schema; | ||
|
||
import io.smallrye.openapi.runtime.util.ModelUtil; | ||
|
||
public class UnusedSchemaFilter implements OASFilter { | ||
|
||
/** | ||
* Map of schemas present in {@code /components/schemas} with a list of the | ||
* schemas that refer to them. | ||
*/ | ||
Map<String, List<Schema>> references = new HashMap<>(); | ||
|
||
@Override | ||
public Schema filterSchema(Schema schema) { | ||
String name = referencedName(schema); | ||
|
||
if (name != null) { | ||
references.computeIfAbsent(name, k -> new ArrayList<>()).add(schema); | ||
} | ||
|
||
return schema; | ||
} | ||
|
||
@Override | ||
public void filterOpenAPI(OpenAPI openAPI) { | ||
final Components components = openAPI.getComponents(); | ||
|
||
Optional.ofNullable(components) | ||
.map(Components::getSchemas) | ||
.map(Map::keySet) | ||
.ifPresent(schemaNames -> { | ||
Set<String> unusedNames = unusedSchemaNames(schemaNames); | ||
|
||
while (!unusedNames.isEmpty()) { | ||
unusedNames.forEach(name -> remove(name, components)); | ||
unusedNames = unusedSchemaNames(schemaNames); | ||
} | ||
}); | ||
} | ||
|
||
String referencedName(Schema schema) { | ||
final String ref = schema.getRef(); | ||
|
||
if (ref != null && ref.startsWith("#/components/schemas/")) { | ||
return ModelUtil.nameFromRef(ref); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
boolean notUsed(String schemaName) { | ||
return !references.containsKey(schemaName); | ||
} | ||
|
||
Set<String> unusedSchemaNames(Set<String> allSchemaNames) { | ||
return allSchemaNames.stream().filter(this::notUsed).collect(Collectors.toSet()); | ||
} | ||
|
||
void remove(String schemaName, Components components) { | ||
Schema unusedSchema = components.getSchemas().get(schemaName); | ||
removeReference(unusedSchema.getAdditionalPropertiesSchema()); | ||
removeReferences(unusedSchema.getAllOf()); | ||
removeReferences(unusedSchema.getAnyOf()); | ||
removeReferences(unusedSchema.getOneOf()); | ||
removeReference(unusedSchema.getItems()); | ||
removeReference(unusedSchema.getNot()); | ||
removeReferences(unusedSchema.getProperties()); | ||
components.removeSchema(schemaName); | ||
UtilLogging.logger.unusedSchemaRemoved(schemaName); | ||
} | ||
|
||
void removeReference(Schema schema) { | ||
if (schema != null) { | ||
String name = referencedName(schema); | ||
|
||
if (name != null) { | ||
references.computeIfPresent(name, (k, v) -> { | ||
v.remove(schema); | ||
return v.isEmpty() ? null : v; | ||
}); | ||
} | ||
} | ||
} | ||
|
||
void removeReferences(Map<String, Schema> schemas) { | ||
if (schemas != null) { | ||
schemas.values().forEach(this::removeReference); | ||
} | ||
} | ||
|
||
void removeReferences(List<Schema> schemas) { | ||
if (schemas != null) { | ||
schemas.forEach(this::removeReference); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
core/src/test/java/io/smallrye/openapi/api/util/UnusedSchemaFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package io.smallrye.openapi.api.util; | ||
|
||
import static org.eclipse.microprofile.openapi.OASFactory.createAPIResponse; | ||
import static org.eclipse.microprofile.openapi.OASFactory.createAPIResponses; | ||
import static org.eclipse.microprofile.openapi.OASFactory.createComponents; | ||
import static org.eclipse.microprofile.openapi.OASFactory.createContent; | ||
import static org.eclipse.microprofile.openapi.OASFactory.createMediaType; | ||
import static org.eclipse.microprofile.openapi.OASFactory.createOpenAPI; | ||
import static org.eclipse.microprofile.openapi.OASFactory.createOperation; | ||
import static org.eclipse.microprofile.openapi.OASFactory.createPathItem; | ||
import static org.eclipse.microprofile.openapi.OASFactory.createPaths; | ||
import static org.eclipse.microprofile.openapi.OASFactory.createSchema; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.eclipse.microprofile.openapi.models.OpenAPI; | ||
import org.eclipse.microprofile.openapi.models.media.Schema.SchemaType; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class UnusedSchemaFilterTest { | ||
|
||
UnusedSchemaFilter target; | ||
OpenAPI openAPI; | ||
|
||
@BeforeEach | ||
void setUp() throws Exception { | ||
target = new UnusedSchemaFilter(); | ||
|
||
openAPI = createOpenAPI(); | ||
openAPI.paths(createPaths() | ||
.addPathItem("/data", createPathItem() | ||
.GET(createOperation() | ||
.responses(createAPIResponses() | ||
.addAPIResponse("200", createAPIResponse() | ||
.content(createContent() | ||
.addMediaType("text/plain", createMediaType() | ||
.schema(createSchema().ref("#/components/schemas/Data"))) | ||
.addMediaType("text/html", createMediaType() | ||
.schema(createSchema().ref( | ||
"http://example.com/schemas/Data?type=html"))))))))) | ||
.components(createComponents() | ||
.addSchema("Data", createSchema() | ||
.type(SchemaType.STRING) | ||
.description("The data returned by the API"))); | ||
} | ||
|
||
@Test | ||
void testUnusedSchemaPropertyRemoved() { | ||
openAPI.getComponents() | ||
.addSchema("RemovedSchema", createSchema() | ||
.type(SchemaType.OBJECT) | ||
.description("Schema to be removed, pass 1") | ||
.addProperty("prop1", createSchema() | ||
.ref("#/components/schemas/RemovedPropertySchema")) | ||
.addProperty("prop2", createSchema() | ||
.ref("#/components/schemas/RemovedPropertySchema"))) | ||
.addSchema("RemovedPropertySchema", createSchema() | ||
.type(SchemaType.STRING) | ||
.description("Schema to be removed, pass 2")); | ||
|
||
assertEquals(3, openAPI.getComponents().getSchemas().size()); | ||
|
||
openAPI = FilterUtil.applyFilter(target, openAPI); | ||
assertEquals(1, openAPI.getComponents().getSchemas().size()); | ||
assertEquals("Data", openAPI.getComponents().getSchemas().keySet().iterator().next()); | ||
} | ||
|
||
@Test | ||
void testUnusedOneOfSchemasRemoved() { | ||
openAPI.getComponents() | ||
.addSchema("RemovedSchema", createSchema() | ||
.type(SchemaType.OBJECT) | ||
.description("Schema to be removed, pass 1") | ||
.addOneOf(createSchema() | ||
.ref("#/components/schemas/RemovedAllOfSchema1")) | ||
.addOneOf(createSchema() | ||
.ref("#/components/schemas/RemovedAllOfSchema2")) | ||
.addOneOf(createSchema() | ||
.type(SchemaType.BOOLEAN))) | ||
.addSchema("RemovedAllOfSchema1", createSchema() | ||
.type(SchemaType.INTEGER) | ||
.description("Schema to be removed, pass 2")) | ||
.addSchema("RemovedAllOfSchema2", createSchema() | ||
.type(SchemaType.STRING) | ||
.description("Schema to be removed, pass 2")); | ||
|
||
assertEquals(4, openAPI.getComponents().getSchemas().size()); | ||
|
||
openAPI = FilterUtil.applyFilter(target, openAPI); | ||
assertEquals(1, openAPI.getComponents().getSchemas().size()); | ||
assertEquals("Data", openAPI.getComponents().getSchemas().keySet().iterator().next()); | ||
} | ||
|
||
} |