-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
400 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,40 @@ | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.github.victools.jsonschema.generator.OptionPreset; | ||
import com.github.victools.jsonschema.generator.SchemaGenerator; | ||
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; | ||
import com.github.victools.jsonschema.generator.SchemaVersion; | ||
import com.github.victools.jsonschema.module.jackson.JacksonModule; | ||
|
||
import com.github.victools.jsonschema.module.jackson.JacksonOption; | ||
import ru.pulsar.jenkins.library.configuration.JobConfiguration; | ||
|
||
import java.io.File; | ||
import java.io.StringWriter; | ||
import java.io.BufferedWriter; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public class JobConfigurationSchemaGenerator { | ||
|
||
public static void main(String[] args) throws Exception { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
JsonSchemaGenerator generator = new JsonSchemaGenerator(mapper); | ||
JsonSchema jsonSchema = generator.generateSchema(JobConfiguration.class); | ||
|
||
StringWriter json = new StringWriter(); | ||
mapper.configure(SerializationFeature.INDENT_OUTPUT, true); | ||
mapper.writeValue(json, jsonSchema); | ||
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON) | ||
.with(new JacksonModule(JacksonOption.FLATTENED_ENUMS_FROM_JSONVALUE, JacksonOption.FLATTENED_ENUMS_FROM_JSONPROPERTY)); | ||
|
||
configBuilder.forFields().withDefaultResolver(field -> { | ||
JsonProperty annotation = field.getAnnotationConsideringFieldAndGetter(JsonProperty.class); | ||
return annotation == null || annotation.defaultValue().isEmpty() ? null : annotation.defaultValue(); | ||
}); | ||
|
||
File jsonSchemaFile = new File("./resources/schema.json"); | ||
mapper.writeValue(jsonSchemaFile, jsonSchema); | ||
SchemaGenerator generator = new SchemaGenerator(configBuilder.build()); | ||
JsonNode jsonSchema = generator.generateSchema(JobConfiguration.class); | ||
|
||
System.out.println(json); | ||
String outputPath = "./resources/schema.json"; | ||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath))) { | ||
writer.write(jsonSchema.toPrettyString()); | ||
System.out.println(jsonSchema.toPrettyString()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters