Skip to content

Commit

Permalink
Merge pull request #1 from rlratcliffe/fix-path-collision
Browse files Browse the repository at this point in the history
Fix path collision and allow releases under ms3 fork
  • Loading branch information
rlratcliffe authored Apr 18, 2024
2 parents e298b5f + 624a49b commit 8087e8e
Show file tree
Hide file tree
Showing 8 changed files with 429 additions and 48 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/github-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Publish package to GitHub Packages
on:
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0

- name: Publish package
run: ./gradlew publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE: true
28 changes: 14 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ allprojects {
apply plugin: 'jacoco'

group = "org.openapi4j"
version = "1.0.7" + (Boolean.valueOf(System.getProperty("release")) ? "" : "-SNAPSHOT")
version = "MS3-1.0.8" + (Boolean.valueOf(System.getenv("RELEASE")) ? "" : "-SNAPSHOT")

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down Expand Up @@ -45,27 +45,27 @@ allprojects {
// Only report code coverage for published projects
def publishedProjects = subprojects.findAll { it.path != ':openapi-perf-checker' }

task jacocoMerge(type: JacocoMerge) {
publishedProjects.each { subproject ->
executionData subproject.tasks.withType(Test)
tasks.register("jacocoMerge", JacocoReport) {
dependsOn {
publishedProjects.collect { it.tasks.withType(Test) }
}
doFirst {
executionData = files(executionData.findAll { it.exists() })
executionData = files(executionData.files.findAll { it.exists() })
}
}

task codeCoverageReport(type: JacocoReport) {
dependsOn publishedProjects.test, jacocoMerge
tasks.register("codeCoverageReport", JacocoReport) {
dependsOn(publishedProjects.collect { it.tasks.named("test") }, "jacocoMerge")

additionalSourceDirs.from = files(publishedProjects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(publishedProjects.sourceSets.main.allSource.srcDirs)
classDirectories.from = files(publishedProjects.sourceSets.main.output)
executionData jacocoMerge.destinationFile
additionalSourceDirs.from(publishedProjects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from(publishedProjects.sourceSets.main.allSource.srcDirs)
classDirectories.from(publishedProjects.sourceSets.main.output)
executionData(jacocoMerge.get().destinationFile)

reports {
xml.enabled true
html.enabled true
csv.enabled false
xml.enabled(true)
html.enabled(true)
csv.enabled(false)
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Sat Nov 02 20:18:41 CET 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
implementation("io.undertow:undertow-core:2.0.1.Final")

testImplementation("junit:junit:4.13.2")
testImplementation("org.mockito:mockito-inline:3.1.0")
testImplementation("org.mockito:mockito-core:5.11.0")
}

apply from: "../../publish.gradle"
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import org.openapi4j.schema.validator.ValidationContext;
import org.openapi4j.schema.validator.ValidationData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -316,9 +313,32 @@ private void validateResponse(final Response response,
}

private Map<Pattern, Path> buildPathPatterns() {
Map<Pattern, Path> patterns = new HashMap<>();
Map<Pattern, Path> patterns = new LinkedHashMap<>();

for (Map.Entry<String, Path> pathEntry : openApi.getPaths().entrySet()) {
Map<String, Path> originalPaths = openApi.getPaths();
List<Map.Entry<String, Path> > originalPathsAsList = new LinkedList<>(originalPaths.entrySet());

Comparator<Map.Entry<String, Path>> pathComparator = (entry1, entry2) -> {
boolean key1ContainsCurlyBrace = entry1.getKey().contains("{");
boolean key2ContainsCurlyBrace = entry2.getKey().contains("{");

if (!key1ContainsCurlyBrace && key2ContainsCurlyBrace) {
return -1;
} else if (key1ContainsCurlyBrace && !key2ContainsCurlyBrace) {
return 1;
} else {
return 0;
}
};

Collections.sort(originalPathsAsList, pathComparator);

HashMap<String, Path> sortedPaths = new LinkedHashMap<>();
for (Map.Entry<String, Path> entry : originalPathsAsList) {
sortedPaths.put(entry.getKey(), entry.getValue());
}

for (Map.Entry<String, Path> pathEntry : sortedPaths.entrySet()) {
List<Pattern> builtPathPatterns = PathResolver.instance().buildPathPatterns(
openApi.getContext(),
openApi.getServers(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ public void withServerPathFindOperationCheck() throws Exception {
true);
}

@Test
// if Map<Pattern, Path> patterns in RequestValidator.buildPathPatterns() is initialized as a map that doesn't retain order of elements then this test can produce unreliable results
// Multiple resources on /greeting/{id} in yaml should reduce the chance
public void withPathCollision() throws Exception {
URL specPath = RequestValidatorTest.class.getResource("/request/pathCollision.yaml");
OpenApi3 api = new OpenApi3Parser().parse(specPath, false);
RequestValidator requestValidator = new RequestValidator(api);

checkRequest(
api,
"greetingBySpanishResource",
requestValidator,
new DefaultRequest.Builder("https://api.com/greeting/spanish", GET).build(),
true);
}

@Test
public void responseTest() throws Exception {
URL specPath = RequestValidatorTest.class.getResource("/request/requestValidator-with-servers.yaml");
Expand Down
Loading

0 comments on commit 8087e8e

Please sign in to comment.