Skip to content

Commit

Permalink
Fixed issue with .. jsonpath operator; fixes gh-1754
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Apr 26, 2023
2 parents c04b110 + 7fce11b commit c6f948c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -93,33 +94,35 @@ private StringBuilder adocWithMetadata(List<Class> metadata) throws Exception {
mapper.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
StringBuilder sb = new StringBuilder();
for (Class metadatum : metadata) {
Constructor constructor = null;
try {
SpringCloudContractMetadata newInstance = (SpringCloudContractMetadata) metadatum
.getDeclaredConstructor(null).newInstance();
String description = newInstance.description();
String key = newInstance.key();
List<Class> additionalClasses = classesToLookAt(metadatum, newInstance);
// @formatter:off
sb
.append("[[metadata-").append(key).append("]]\n")
.append("##### Metadata `").append(key).append("`\n\n")
.append("* key: `").append(key).append("`").append("\n")
.append("* description:\n\n").append(description).append("\n\n")
.append("Example:\n\n")
.append("```yaml\n").append(mapper.writeValueAsString(newInstance)).append("\n```\n\n")
// To make the schema collapsable
.append("+++ <details><summary> +++\nClick here to expand the JSON schema:\n+++ </summary><div> +++\n")
.append("```json\n").append(generateJsonSchemaForClass(metadatum)).append("\n```\n")
.append("+++ </div></details> +++\n\n")
.append("If you are interested in learning more about the types and its properties, check out the following classes:\n\n")
.append(additionalClasses.stream().map(aClass -> "* `" + aClass.getName() + "`").collect(Collectors.joining("\n")))
.append("\n\n");
// @formatter:on
constructor = metadatum.getDeclaredConstructor(null);
}
catch (Exception exception) {
log.error("Exception occurred while trying to parse [" + metadatum + "]");
throw exception;
catch (Exception ex) {
log.warn("Failed to find a no-args constructor for matadatum [" + metadatum + "]", ex);
continue;
}
Object instance = constructor.newInstance();
SpringCloudContractMetadata newInstance = (SpringCloudContractMetadata) instance;
String description = newInstance.description();
String key = newInstance.key();
List<Class> additionalClasses = classesToLookAt(metadatum, newInstance);
// @formatter:off
sb
.append("[[metadata-").append(key).append("]]\n")
.append("##### Metadata `").append(key).append("`\n\n")
.append("* key: `").append(key).append("`").append("\n")
.append("* description:\n\n").append(description).append("\n\n")
.append("Example:\n\n")
.append("```yaml\n").append(mapper.writeValueAsString(newInstance)).append("\n```\n\n")
// To make the schema collapsable
.append("+++ <details><summary> +++\nClick here to expand the JSON schema:\n+++ </summary><div> +++\n")
.append("```json\n").append(generateJsonSchemaForClass(metadatum)).append("\n```\n")
.append("+++ </div></details> +++\n\n")
.append("If you are interested in learning more about the types and its properties, check out the following classes:\n\n")
.append(additionalClasses.stream().map(aClass -> "* `" + aClass.getName() + "`").collect(Collectors.joining("\n")))
.append("\n\n");
// @formatter:on
}
return sb;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ import org.springframework.cloud.contract.spec.internal.ExecutionProperty
import org.springframework.cloud.contract.spec.internal.MatchingType
import org.springframework.cloud.contract.spec.internal.OptionalProperty
import org.springframework.cloud.contract.spec.internal.RegexProperty
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
import org.springframework.util.SerializationUtils

/**
* I would like to apologize to anyone who is reading this class. Since JSON is a hectic structure
* this class is also hectic. The idea is to traverse the JSON structure and build a set of
Expand Down Expand Up @@ -157,7 +155,6 @@ class JsonToJsonPathsConverter {
containsOnlyEmptyElements(object)
&& isNotRootArray(matcherPath)) {
String pathToDelete = pathToDelete(pathWithoutAnyArray)
context.delete(pathToDelete)
if (pathToDelete.contains(DESCENDANT_OPERATOR)) {
Object root = context.read('$')
if (rootContainsEmptyContainers(root)) {
Expand All @@ -166,6 +163,8 @@ class JsonToJsonPathsConverter {
return false
}
return false
} else {
context.delete(pathToDelete)
}
return removeTrailingContainers(pathToDelete, context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
package org.springframework.cloud.contract.verifier.builder

import org.junit.Rule
import org.spockframework.runtime.extension.builtin.PreconditionContext
import spock.lang.Ignore
import spock.lang.IgnoreIf
import spock.lang.Issue
import spock.lang.Retry
import spock.lang.Shared
import spock.lang.Specification

Expand Down Expand Up @@ -1763,7 +1759,6 @@ response:
}
}

@Ignore
@Issue("#1414")
def "should work with empty arrays after doing array matching [#methodBuilderName]"() {
given:
Expand Down

0 comments on commit c6f948c

Please sign in to comment.