Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,27 @@ jobs:
java-version: "17"
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew test

regeneration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: "17"
- uses: gradle/actions/setup-gradle@v4
- name: Verify normalization, repeatable generation, and response contracts
run: |
set -euo pipefail
# Freeze one upstream spec for both runs, even if main changes meanwhile.
curl -fsSL https://raw.githubusercontent.com/frain-dev/convoy/main/docs/v3/openapi3.yaml \
-o "$RUNNER_TEMP/openapi3.yaml"
export SPEC_URL="file://$RUNNER_TEMP/openapi3.yaml"
export GENERATOR_JAR="$RUNNER_TEMP/openapi-generator.jar"
./scripts/generate.sh
python3 scripts/test_normalize_spec.py "$GENERATOR_JAR"
cp -R src/main/java "$RUNNER_TEMP/generated-once"
./scripts/generate.sh
diff -ru "$RUNNER_TEMP/generated-once" src/main/java
./gradlew test
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ artifacts change. Locally:
./gradlew test
```

Generation first names the delivery-attempt array alternative in a temporary
copy of the spec and enables an alias model only for that collection. This works around the
Java/native generator's inline-array `oneOf` bug while preserving both response
shapes: `DeliveryAttemptList` for the default array, or
`GetDeliveryAttempts200ResponseAllOfDataOneOf` for a page with `content` and
`next_cursor`. `scripts/NormalizeSpec.java` uses Jackson from the verified
generator JAR; no additional dependencies are required. It also derives a
template override from that JAR so the collection retains `ArrayList`'s
equality/hash behavior rather than the generator's POJO equality behavior.

## License

[MIT](LICENSE)
90 changes: 90 additions & 0 deletions scripts/NormalizeSpec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Keep Java/native generation compatible with the delivery-attempts union.
*
* OpenAPI Generator 7.23.0 emits invalid Java for an inline array in oneOf
* (List<T>.class, getList<T>(), etc.). A named array schema, together with
* x-generate-alias-as-model, uses its supported concrete collection model.
* This only changes the temporary generation input, never the wire schema.
* Jackson/YAML are provided by the checksum-verified generator JAR, so this
* source-file launcher needs no additional build tool or dependency download.
*/
class NormalizeSpec {
private static final String ARRAY_MODEL = "DeliveryAttemptList";

public static void main(String[] args) throws Exception {
ObjectMapper yaml = new ObjectMapper(new YAMLFactory());
ObjectNode spec = (ObjectNode) yaml.readTree(Path.of(args[0]).toFile());
ObjectNode schemas = (ObjectNode) spec.path("components").path("schemas");

for (JsonNode path : spec.path("paths")) {
JsonNode operation = path.path("get");
if (!"GetDeliveryAttempts".equals(operation.path("operationId").asText())) {
continue;
}
JsonNode response = operation.path("responses").path("200")
.path("content").path("application/json").path("schema");
normalizeResponse(response, schemas);
}

new ObjectMapper().writerWithDefaultPrettyPrinter()
.writeValue(Path.of(args[1]).toFile(), spec);
prepareTemplates(Path.of(args[2]));
}

private static void normalizeResponse(JsonNode response, ObjectNode schemas) {
JsonNode variants = response.path("properties").path("data").path("oneOf");
if (variants.isArray()) {
ArrayNode oneOf = (ArrayNode) variants;
for (int i = 0; i < oneOf.size(); i++) {
JsonNode variant = oneOf.get(i);
if (!"array".equals(variant.path("type").asText())) {
continue;
}
JsonNode existing = schemas.get(ARRAY_MODEL);
if (existing != null && !existing.equals(variant)) {
throw new IllegalArgumentException("Conflicting schema: " + ARRAY_MODEL);
}
// Preserve items, constraints and descriptions verbatim.
ObjectNode array = variant.deepCopy();
array.put("x-generate-alias-as-model", true);
array.put("x-convoy-array-alias", true);
schemas.set(ARRAY_MODEL, array);
oneOf.set(i, schemas.objectNode().put("$ref", "#/components/schemas/" + ARRAY_MODEL));
}
}
for (JsonNode component : response.path("allOf")) {
normalizeResponse(component, schemas);
}
}

private static void prepareTemplates(Path directory) throws Exception {
// The stock POJO equality/hash template violates the List contract for
// array aliases. Keep ArrayList's implementations for this alias only.
// Read the pinned template from the verified JAR instead of vendoring
// or silently patching generated Java. Fail closed if its layout moves.
String template;
try (var source = NormalizeSpec.class.getResourceAsStream("/Java/libraries/native/pojo.mustache")) {
template = new String(source.readAllBytes(), StandardCharsets.UTF_8);
}
int start = template.indexOf(" /**\n * Return true if this {{name}} object is equal to o.");
int end = template.indexOf(" @Override\n public String toString()", start);
if (start < 0 || end < start) {
throw new IllegalStateException("Generator POJO equality template changed; review the array alias workaround");
}
String guard = "vendorExtensions.x-convoy-array-alias";
template = template.substring(0, start) + "{{^" + guard + "}}\n"
+ template.substring(start, end) + "{{/" + guard + "}}\n" + template.substring(end);
Path target = directory.resolve("libraries/native/pojo.mustache");
Files.createDirectories(target.getParent());
Files.writeString(target, template);
}
}
13 changes: 12 additions & 1 deletion scripts/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,26 @@ echo "${GENERATOR_SHA256} ${GENERATOR_JAR}" | shasum -a 256 -c - >/dev/null ||

curl -fsSL "$SPEC_URL" -o "$tmp/openapi3.yaml"

# Name the delivery-attempt array alternative in our temporary input. The
# native generator cannot compile inline array alternatives in oneOf, but
# supports named collection models without changing either JSON wire shape.
java -cp "$GENERATOR_JAR" scripts/NormalizeSpec.java \
"$tmp/openapi3.yaml" "$tmp/openapi3.json" "$tmp/templates"

# hideGenerationTimestamp: @Generated annotations must not embed a wall-clock
# date or every regeneration diffs all 200+ files and "no diff, no PR" breaks.
java -jar "$GENERATOR_JAR" generate \
-i "$tmp/openapi3.yaml" \
-i "$tmp/openapi3.json" \
-g java \
--library native \
-t "$tmp/templates" \
-o "$tmp/gen" \
--additional-properties=groupId=io.github.frain-dev,artifactId=convoy,apiPackage=com.getconvoy.api,modelPackage=com.getconvoy.models,invokerPackage=com.getconvoy.client,useJakartaEe=true,hideGenerationTimestamp=true

# Restore released method overloads and the list-shaped response API.
# Fail if generator output changes enough to invalidate the compatibility patch.
patch -s -f -F 0 -p1 -d "$tmp/gen" < scripts/preserve-sdk-compatibility.patch

# Mirror only the three generated packages. The hand-written verify package
# (com/getconvoy/webhook) is a sibling and is never created or removed here.
# --delete keeps each generated package an exact mirror of generator output.
Expand Down
Loading
Loading