Render a nested type (enum or record) indented at the given level.
(nested: JavaClassDef, indentLevel: number, nestedTypes: Map<string, JavaClassDef>, allImports: Set<string>)
| 883 | |
| 884 | /** Render a nested type (enum or record) indented at the given level. */ |
| 885 | function renderNestedType(nested: JavaClassDef, indentLevel: number, nestedTypes: Map<string, JavaClassDef>, allImports: Set<string>): string[] { |
| 886 | const ind = " ".repeat(indentLevel); |
| 887 | const lines: string[] = []; |
| 888 | |
| 889 | if (nested.kind === "enum") { |
| 890 | lines.push(""); |
| 891 | if (nested.description) { |
| 892 | lines.push(`${ind}/** ${nested.description} */`); |
| 893 | } |
| 894 | lines.push(`${ind}public enum ${nested.name} {`); |
| 895 | for (let i = 0; i < (nested.values || []).length; i++) { |
| 896 | const v = nested.values![i]; |
| 897 | const comma = i < nested.values!.length - 1 ? "," : ";"; |
| 898 | lines.push(`${ind} /** The {@code ${v}} variant. */`); |
| 899 | lines.push(`${ind} ${toEnumConstant(v)}("${v}")${comma}`); |
| 900 | } |
| 901 | lines.push(""); |
| 902 | lines.push(`${ind} private final String value;`); |
| 903 | lines.push(`${ind} ${nested.name}(String value) { this.value = value; }`); |
| 904 | lines.push(`${ind} @com.fasterxml.jackson.annotation.JsonValue`); |
| 905 | lines.push(`${ind} public String getValue() { return value; }`); |
| 906 | lines.push(`${ind} @com.fasterxml.jackson.annotation.JsonCreator`); |
| 907 | lines.push(`${ind} public static ${nested.name} fromValue(String value) {`); |
| 908 | lines.push(`${ind} for (${nested.name} v : values()) {`); |
| 909 | lines.push(`${ind} if (v.value.equals(value)) return v;`); |
| 910 | lines.push(`${ind} }`); |
| 911 | lines.push(`${ind} throw new IllegalArgumentException("Unknown ${nested.name} value: " + value);`); |
| 912 | lines.push(`${ind} }`); |
| 913 | lines.push(`${ind}}`); |
| 914 | } else if (nested.kind === "class" && nested.schema?.properties) { |
| 915 | const localNestedTypes = new Map<string, JavaClassDef>(); |
| 916 | const fields: { jsonName: string; javaName: string; javaType: string; description?: string }[] = []; |
| 917 | |
| 918 | for (const [propName, propSchema] of Object.entries(nested.schema.properties)) { |
| 919 | if (typeof propSchema !== "object") continue; |
| 920 | const prop = propSchema as JSONSchema7; |
| 921 | // Record components are always boxed (nullable by design). |
| 922 | const result = schemaTypeToJava(prop, false, nested.name, propName, localNestedTypes); |
| 923 | for (const imp of result.imports) allImports.add(imp); |
| 924 | fields.push({ jsonName: propName, javaName: toCamelCase(propName), javaType: result.javaType, description: prop.description }); |
| 925 | } |
| 926 | |
| 927 | lines.push(""); |
| 928 | if (nested.description) { |
| 929 | lines.push(`${ind}/** ${nested.description} */`); |
| 930 | } |
| 931 | lines.push(`${ind}@JsonIgnoreProperties(ignoreUnknown = true)`); |
| 932 | lines.push(`${ind}@JsonInclude(JsonInclude.Include.NON_NULL)`); |
| 933 | if (fields.length === 0) { |
| 934 | lines.push(`${ind}public record ${nested.name}() {`); |
| 935 | } else { |
| 936 | lines.push(`${ind}public record ${nested.name}(`); |
| 937 | for (let i = 0; i < fields.length; i++) { |
| 938 | const f = fields[i]; |
| 939 | const comma = i < fields.length - 1 ? "," : ""; |
| 940 | if (f.description) lines.push(`${ind} /** ${f.description} */`); |
| 941 | lines.push(`${ind} @JsonProperty("${f.jsonName}") ${f.javaType} ${f.javaName}${comma}`); |
| 942 | } |
no test coverage detected
searching dependent graphs…