• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

torand / jsonschema2java / 18399814123

10 Oct 2025 07:35AM UTC coverage: 78.214% (-0.9%) from 79.157%
18399814123

push

github

torand
fix: bean validation annotations on primitive subtypes of compound pojo property types now generated

287 of 413 branches covered (69.49%)

Branch coverage included in aggregate %.

440 of 533 new or added lines in 26 files covered. (82.55%)

1 existing line in 1 file now uncovered.

808 of 987 relevant lines covered (81.86%)

4.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.24
/src/main/java/io/github/torand/jsonschema2java/generators/ModelGenerator.java
1
/*
2
 * Copyright (c) 2024-2025 Tore Eide Andersen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.torand.jsonschema2java.generators;
17

18
import io.github.torand.jsonschema2java.collectors.EnumInfoCollector;
19
import io.github.torand.jsonschema2java.collectors.PojoInfoCollector;
20
import io.github.torand.jsonschema2java.collectors.SchemaResolver;
21
import io.github.torand.jsonschema2java.model.EnumInfo;
22
import io.github.torand.jsonschema2java.model.PojoInfo;
23
import io.github.torand.jsonschema2java.utils.JsonSchema2JavaException;
24
import io.github.torand.jsonschema2java.utils.JsonSchemaDef;
25
import io.github.torand.jsonschema2java.writers.EnumWriter;
26
import io.github.torand.jsonschema2java.writers.PojoWriter;
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29

30
import java.io.IOException;
31
import java.nio.file.Path;
32
import java.util.List;
33

34
import static io.github.torand.jsonschema2java.utils.StringUtils.pluralSuffix;
35
import static io.github.torand.jsonschema2java.writers.WriterFactory.createEnumWriter;
36
import static io.github.torand.jsonschema2java.writers.WriterFactory.createPojoWriter;
37

38
/**
39
 * Generates source code for models (pojos).
40
 */
41
public class ModelGenerator {
42
    private static final Logger logger = LoggerFactory.getLogger(ModelGenerator.class);
4✔
43

44
    private final Options opts;
45
    private final SchemaResolver schemaResolver;
46

47
    public ModelGenerator(Options opts) {
2✔
48
        this.opts = opts;
3✔
49
        this.schemaResolver = new SchemaResolver(opts);
6✔
50
    }
1✔
51

52
    public void generate(List<Path> schemaFiles) {
53
        int enumCount = 0;
2✔
54
        int pojoCount = 0;
2✔
55

56
        for (Path schemaFile : schemaFiles) {
10✔
57
            JsonSchemaDef schema = schemaResolver.load(schemaFile);
5✔
58
            String pojoName = schema.getName() + opts.pojoNameSuffix();
7✔
59

60
            if (schema.isEnum()) {
3✔
61
                generateEnumFile(pojoName, schema);
4✔
62
                enumCount++;
1✔
63
            }
64

65
            if (schema.isClass()) {
3✔
66
                generatePojoFile(pojoName, schema);
4✔
67
                pojoCount++;
1✔
68
            }
69
        }
1✔
70

71
        logger.info("Generated {} enum{}, {} pojo{} in directory {}", enumCount, pluralSuffix(enumCount), pojoCount, pluralSuffix(pojoCount), opts.getModelOutputDir(null));
32✔
72
    }
1✔
73

74
    private void generateEnumFile(String name, JsonSchemaDef schema) {
75
        if (opts.verbose()) {
4!
76
            logger.info("Generating model enum {}", name);
4✔
77
        }
78

79
        EnumInfoCollector enumInfoCollector = new EnumInfoCollector(opts);
6✔
80
        EnumInfo enumInfo = enumInfoCollector.getEnumInfo(name, schema);
5✔
81

82
        String enumFilename = name + opts.getFileExtension();
6✔
83
        try (EnumWriter enumWriter = createEnumWriter(enumFilename, opts, enumInfo.modelSubdir())) {
7✔
84
            enumWriter.write(enumInfo);
3✔
85
        } catch (IOException e) {
×
NEW
86
            throw new JsonSchema2JavaException("Failed to write file %s".formatted(enumFilename), e);
×
87
        }
1✔
88
    }
1✔
89

90
    private void generatePojoFile(String name, JsonSchemaDef schema) {
91
        if (opts.verbose()) {
4!
92
            logger.info("Generating model class {}", name);
4✔
93
        }
94

95
        PojoInfoCollector pojoInfoCollector = new PojoInfoCollector(opts, schemaResolver);
8✔
96
        PojoInfo pojoInfo = pojoInfoCollector.getPojoInfo(name, schema);
5✔
97

98
        String pojoFilename = name + opts.getFileExtension();
6✔
99
        try (PojoWriter pojoWriter = createPojoWriter(pojoFilename, opts, pojoInfo.modelSubdir())) {
7✔
100
            pojoWriter.write(pojoInfo);
3✔
101
        } catch (IOException e) {
×
NEW
102
            throw new JsonSchema2JavaException("Failed to write file %s".formatted(pojoFilename), e);
×
103
        }
1✔
104
    }
1✔
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc