-
Notifications
You must be signed in to change notification settings - Fork 5
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
issue-4 #5
issue-4 #5
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,199 @@ | ||||||
package com.ethlo.zally;/*- | ||||||
* #%L | ||||||
* zally-maven-plugin | ||||||
* %% | ||||||
* Copyright (C) 2021 Morten Haraldsen (ethlo) | ||||||
* %% | ||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
* You may obtain a copy of the License at | ||||||
* | ||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||
* | ||||||
* Unless required by applicable law or agreed to in writing, software | ||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
* See the License for the specific language governing permissions and | ||||||
* limitations under the License. | ||||||
* #L% | ||||||
*/ | ||||||
|
||||||
import java.io.File; | ||||||
import java.io.IOException; | ||||||
import java.io.UncheckedIOException; | ||||||
import java.nio.file.Files; | ||||||
import java.nio.file.Paths; | ||||||
import java.util.Collections; | ||||||
import java.util.Iterator; | ||||||
import java.util.LinkedHashMap; | ||||||
import java.util.List; | ||||||
import java.util.Map; | ||||||
import java.util.Optional; | ||||||
|
||||||
import org.apache.maven.plugin.AbstractMojo; | ||||||
import org.apache.maven.plugin.MojoFailureException; | ||||||
import org.apache.maven.plugin.logging.Log; | ||||||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||||||
import org.apache.maven.plugins.annotations.Mojo; | ||||||
import org.apache.maven.plugins.annotations.Parameter; | ||||||
import org.apache.maven.project.MavenProject; | ||||||
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude; | ||||||
import com.fasterxml.jackson.core.JsonPointer; | ||||||
import com.fasterxml.jackson.databind.JsonNode; | ||||||
import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||||||
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; | ||||||
import io.swagger.v3.core.filter.AbstractSpecFilter; | ||||||
import io.swagger.v3.core.filter.SpecFilter; | ||||||
import io.swagger.v3.core.model.ApiDescription; | ||||||
import io.swagger.v3.oas.models.OpenAPI; | ||||||
import io.swagger.v3.oas.models.Operation; | ||||||
import io.swagger.v3.oas.models.PathItem; | ||||||
|
||||||
@Mojo(threadSafe = true, name = "extract", defaultPhase = LifecyclePhase.GENERATE_SOURCES) | ||||||
public class ExtractMojo extends AbstractMojo | ||||||
{ | ||||||
@Parameter(required = true, defaultValue = "${project.basedir}/src/main/resources/api.yaml", property = "zally.source") | ||||||
private String source; | ||||||
|
||||||
@Parameter(property = "targetFile") | ||||||
private File targetFile; | ||||||
|
||||||
@Parameter(property = "zally.skip", defaultValue = "false") | ||||||
private boolean skip; | ||||||
|
||||||
@Parameter(property = "filters") | ||||||
private List<String> filters; | ||||||
|
||||||
@Parameter(property = "name", required = true) | ||||||
private String name; | ||||||
|
||||||
@Parameter(property = "title") | ||||||
private String title; | ||||||
|
||||||
@Parameter(defaultValue = "${project}", required = true, readonly = true) | ||||||
private MavenProject project; | ||||||
|
||||||
private static final ObjectMapper mapper = new ObjectMapper(); | ||||||
|
||||||
@Override | ||||||
public void execute() throws MojoFailureException | ||||||
{ | ||||||
final Optional<OpenAPI> loaded = load(getLog(), skip, source); | ||||||
|
||||||
if (targetFile == null) | ||||||
{ | ||||||
targetFile = Paths.get(project.getBuild().getOutputDirectory()).resolve(name + ".yaml").toFile(); | ||||||
} | ||||||
|
||||||
loaded.ifPresent(openAPI -> | ||||||
{ | ||||||
getLog().info(String.format("Processing filter %s", name)); | ||||||
final OpenAPI filtered = new SpecFilter().filter(openAPI, new AbstractSpecFilter() | ||||||
{ | ||||||
@Override | ||||||
public Optional<Operation> filterOperation(final Operation operation, final ApiDescription api, final Map<String, List<String>> params, final Map<String, String> cookies, final Map<String, List<String>> headers) | ||||||
{ | ||||||
final String path = api.getPath(); | ||||||
final PathItem pathItem = openAPI.getPaths().get(path); | ||||||
final Map<String, Object> extensionMap = new LinkedHashMap<>(Optional.ofNullable(pathItem.getExtensions()).orElse(Collections.emptyMap())); | ||||||
extensionMap.putAll(operation.getExtensions()); | ||||||
final OperationData operationData = new OperationData(operation, api, extensionMap); | ||||||
if (filters.stream().anyMatch(filter -> match(filter, operationData))) | ||||||
{ | ||||||
getLog().info(String.format("Including operation %s in %s", operation.getOperationId(), name)); | ||||||
return Optional.of(operation); | ||||||
} | ||||||
return Optional.empty(); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public boolean isRemovingUnreferencedDefinitions() | ||||||
{ | ||||||
return true; | ||||||
} | ||||||
}, null, null, null); | ||||||
|
||||||
|
||||||
// Set title of document | ||||||
Optional.ofNullable(title).ifPresent(t -> filtered.getInfo().setTitle(t)); | ||||||
|
||||||
try | ||||||
{ | ||||||
getLog().info("Writing extracted APIs to " + targetFile); | ||||||
Files.createDirectories(targetFile.toPath().getParent()); | ||||||
new ObjectMapper(new YAMLFactory() | ||||||
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)). | ||||||
setSerializationInclusion(JsonInclude.Include.NON_EMPTY).writeValue(targetFile, filtered); | ||||||
} | ||||||
catch (IOException e) | ||||||
{ | ||||||
throw new UncheckedIOException(e); | ||||||
} | ||||||
}); | ||||||
} | ||||||
|
||||||
public static boolean match(final String filter, final OperationData data) | ||||||
{ | ||||||
final String[] expression = filter.split("="); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. StringSplitter: String.split(String) has surprising behavior (details)
Suggested change
(at-me in a reply with Was this a good recommendation? |
||||||
final String path = expression[0].startsWith("/") ? expression[0] : "/" + expression[0]; | ||||||
final String regexp = expression[1]; | ||||||
try | ||||||
{ | ||||||
final JsonNode jsonNode = mapper.readTree(mapper.writeValueAsString(data)); | ||||||
final JsonNode value = jsonNode.at(JsonPointer.compile(path)); | ||||||
if (value.isObject()) | ||||||
{ | ||||||
final Iterator<String> fieldNames = value.fieldNames(); | ||||||
while (fieldNames.hasNext()) | ||||||
{ | ||||||
if (fieldNames.next().matches(regexp)) | ||||||
{ | ||||||
return true; | ||||||
} | ||||||
} | ||||||
} | ||||||
else if (value.isArray()) | ||||||
{ | ||||||
final Iterator<JsonNode> elements = value.elements(); | ||||||
while (elements.hasNext()) | ||||||
{ | ||||||
if (elements.next().textValue().matches(regexp)) | ||||||
{ | ||||||
return true; | ||||||
} | ||||||
} | ||||||
} | ||||||
else if (value.isTextual()) | ||||||
{ | ||||||
return value.textValue().matches(regexp); | ||||||
} | ||||||
|
||||||
return false; | ||||||
} | ||||||
catch (IOException exc) | ||||||
{ | ||||||
throw new UncheckedIOException(exc); | ||||||
} | ||||||
} | ||||||
|
||||||
public static Optional<OpenAPI> load(final Log log, final boolean skip, final String source) throws MojoFailureException | ||||||
{ | ||||||
if (skip) | ||||||
{ | ||||||
log.info("Skipping execution as requested"); | ||||||
return Optional.empty(); | ||||||
} | ||||||
|
||||||
final boolean existsOnClassPath = ExtractMojo.class.getClassLoader().getResourceAsStream(source) != null; | ||||||
final boolean existsOnFilesystem = Files.exists(Paths.get(source)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PATH_TRAVERSAL_IN: This API (java/nio/file/Paths.get(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;) reads a file whose location might be specified by user input (details) (at-me in a reply with Was this a good recommendation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PATH_TRAVERSAL_IN: This API (java/nio/file/Paths.get(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;) reads a file whose location might be specified by user input (details) (at-me in a reply with Was this a good recommendation? |
||||||
if (!existsOnClassPath && !existsOnFilesystem) | ||||||
{ | ||||||
throw new MojoFailureException("The specified source file could not be found: " + source); | ||||||
} | ||||||
|
||||||
log.info("Reading file '" + source + "'"); | ||||||
return Optional.of(new OpenApiParser().parse(source)); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.ethlo.zally; | ||
|
||
/*- | ||
* #%L | ||
* zally-maven-plugin | ||
* %% | ||
* Copyright (C) 2021 - 2022 Morten Haraldsen (ethlo) | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import io.swagger.v3.core.model.ApiDescription; | ||
import io.swagger.v3.oas.models.Operation; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OperationData | ||
{ | ||
private final Map<String, Object> extensions; | ||
private final String method; | ||
private final String path; | ||
private final Boolean deprecated; | ||
private final String operationId; | ||
private final List<String> tags; | ||
|
||
public OperationData(final Operation operation, final ApiDescription api, final Map<String, Object> extensions) | ||
{ | ||
this.method = api.getMethod(); | ||
this.path = api.getPath(); | ||
this.deprecated = operation.getDeprecated(); | ||
this.operationId = operation.getOperationId(); | ||
this.tags = operation.getTags(); | ||
this.extensions = extensions; | ||
} | ||
|
||
public Map<String, Object> getExtensions() | ||
{ | ||
return extensions; | ||
} | ||
|
||
public String getMethod() | ||
{ | ||
return method; | ||
} | ||
|
||
public String getPath() | ||
{ | ||
return path; | ||
} | ||
|
||
public Boolean getDeprecated() | ||
{ | ||
return deprecated; | ||
} | ||
|
||
public String getOperationId() | ||
{ | ||
return operationId; | ||
} | ||
|
||
public List<String> getTags() | ||
{ | ||
return tags; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PATH_TRAVERSAL_IN: This API (java/nio/file/Paths.get(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;) reads a file whose location might be specified by user input (details)
(at-me in a reply with
help
orignore
)Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]