|
29 | 29 | import io.swagger.v3.oas.models.parameters.Parameter;
|
30 | 30 | import io.swagger.v3.oas.models.security.*;
|
31 | 31 | import io.swagger.v3.oas.models.tags.Tag;
|
| 32 | +import org.apache.commons.io.comparator.PathFileComparator; |
32 | 33 | import org.apache.commons.lang3.ObjectUtils;
|
33 | 34 | import org.apache.commons.lang3.StringUtils;
|
34 | 35 | import org.openapitools.codegen.api.TemplatePathLocator;
|
|
57 | 58 | import java.nio.file.Path;
|
58 | 59 | import java.time.ZonedDateTime;
|
59 | 60 | import java.util.*;
|
60 |
| -import java.util.stream.Stream; |
61 | 61 |
|
62 | 62 | import static org.openapitools.codegen.utils.OnceLogger.once;
|
63 | 63 |
|
@@ -913,26 +913,36 @@ public List<File> generate() {
|
913 | 913 | System.err.println(sb.toString());
|
914 | 914 | } else {
|
915 | 915 | if (generateMetadata) {
|
916 |
| - StringBuilder sb = new StringBuilder(); |
917 |
| - File outDir = new File(this.config.getOutputDir()); |
918 |
| - Optional.of(files) |
919 |
| - .map(Collection::stream) |
920 |
| - .orElseGet(Stream::empty) |
921 |
| - .filter(Objects::nonNull) |
922 |
| - .map(File::toPath) |
923 |
| - .sorted(Path::compareTo) |
924 |
| - .forEach(f -> { |
925 |
| - String relativePath = java.nio.file.Paths.get(outDir.toURI()).relativize(f).toString(); |
926 |
| - if (!relativePath.equals(METADATA_DIR + File.separator + "VERSION")) { |
927 |
| - sb.append(relativePath).append(System.lineSeparator()); |
928 |
| - } |
929 |
| - }); |
930 |
| - |
931 |
| - String targetFile = config.outputFolder() + File.separator + METADATA_DIR + File.separator + "FILES"; |
932 | 916 | try {
|
| 917 | + StringBuilder sb = new StringBuilder(); |
| 918 | + File outDir = new File(this.config.getOutputDir()); |
| 919 | + |
| 920 | + List<File> filesToSort = new ArrayList<>(); |
| 921 | + |
| 922 | + // Avoid side-effecting sort in this path when generateMetadata=true |
| 923 | + files.forEach(f -> { |
| 924 | + // We have seen NPE on CI for getPath() returning null, so guard against this (to be fixed in 5.0 template management refactor) |
| 925 | + //noinspection ConstantConditions |
| 926 | + if (f != null && f.getPath() != null) { |
| 927 | + filesToSort.add(f); |
| 928 | + } |
| 929 | + }); |
| 930 | + |
| 931 | + filesToSort.sort(PathFileComparator.PATH_COMPARATOR); |
| 932 | + filesToSort.forEach(f -> { |
| 933 | + String relativePath = outDir.toPath().relativize(f.toPath()).toString(); |
| 934 | + if (!relativePath.equals(METADATA_DIR + File.separator + "VERSION")) { |
| 935 | + sb.append(relativePath).append(System.lineSeparator()); |
| 936 | + } |
| 937 | + }); |
| 938 | + |
| 939 | + String targetFile = config.outputFolder() + File.separator + METADATA_DIR + File.separator + "FILES"; |
| 940 | + |
933 | 941 | File filesFile = this.templateProcessor.writeToFile(targetFile, sb.toString().getBytes(StandardCharsets.UTF_8));
|
934 |
| - files.add(filesFile); |
935 |
| - } catch (IOException e) { |
| 942 | + if (filesFile != null) { |
| 943 | + files.add(filesFile); |
| 944 | + } |
| 945 | + } catch (Exception e) { |
936 | 946 | LOGGER.warn("Failed to write FILES metadata to track generated files.");
|
937 | 947 | }
|
938 | 948 | }
|
|
0 commit comments