Skip to content

Init #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "maven" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI

on:
push:
branches:
- '**'
pull_request:
branches:
- '**'

jobs:
build:
runs-on: ubuntu-latest
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'temurin'
- name: Build with Maven
run: ./mvnw --batch-mode clean verify
54 changes: 54 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea

# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml

# Gradle:
.idea/gradle.xml
.idea/libraries

# Mongo Explorer plugin:
.idea/mongoSettings.xml

## File-based project format:
*.iws
*.iml

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Maven template
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties

1 change: 1 addition & 0 deletions .mvn/jvm.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.desktop/java.awt.font=ALL-UNNAMED
1 change: 1 addition & 0 deletions .mvn/wrapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
maven-wrapper.jar
18 changes: 18 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.1/apache-maven-3.8.1-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar
73 changes: 72 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
# json-schema-to-java-record
[![Build Status](https://github.com/Cosium/json-schema-to-java-record/actions/workflows/ci.yml/badge.svg)](https://github.com/Cosium/json-schema-to-java-record/actions/workflows/ci.yml)
[![Maven Central Version](https://img.shields.io/maven-central/v/com.cosium.json_schema_to_java_record/json-schema-to-java-record)](https://central.sonatype.com/artifact/com.cosium.json_schema_to_java_record/json-schema-to-java-record)

# JSON Schema To Java Record

An annotation processor converting JSON schemas to java records.

# Quick start

1. Import the BOM:
```xml
<dependencyManagement>
<dependencies>
<!-- ... -->
<dependency>
<groupId>com.cosium.json_schema_to_java_record</groupId>
<artifactId>json-schema-to-java-record-bom</artifactId>
<version>${json-schema-to-java-record.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- ... -->
</dependencies>
</dependencyManagement>
```
2. Import the API:
```xml
<dependencies>
<!-- ... -->
<dependency>
<groupId>com.cosium.json_schema_to_java_record</groupId>
<artifactId>json-schema-to-java-record-api</artifactId>
</dependency>
<!-- ... -->
</dependencies>
```
3. Import the annotation processor:
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.cosium.json_schema_to_java_record</groupId>
<artifactId>json-schema-to-java-record</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
```
4. Add your JSON schema files to the class path:
```
src/main/resources/com/aqme
└── foo.schema.json
```
5. Annotate a `package-info.java` file like this:
```java
@GenerateRecordsFromJsonSchemas(
schemaRootFileLocations =
@JsonSchemaFileLocation(
moduleAndPackage = "com.aqme",
relativeName = "foo.schema.json"
)
)
package com.aqme;

import com.cosium.json_schema_to_java_record_api.GenerateRecordsFromJsonSchemas;
import com.cosium.json_schema_to_java_record_api.JsonSchemaConfiguration;
import com.cosium.json_schema_to_java_record_api.JsonSchemaFileLocation;
```
6. Compile to generate the java files
14 changes: 14 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.cosium.json_schema_to_java_record</groupId>
<artifactId>json-schema-to-java-record-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<name>JSON schema to Java Record API</name>
<artifactId>json-schema-to-java-record-api</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cosium.json_schema_to_java_record_api;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

/**
* @author Réda Housni Alaoui
*/
@Target({ElementType.PACKAGE})
public @interface GenerateRecordsFromJsonSchemas {

JsonSchemaFileLocation[] schemaRootFileLocations();

JsonSchemaConfiguration[] schemaConfigurations() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cosium.json_schema_to_java_record_api;

/**
* @author Réda Housni Alaoui
*/
public @interface JsonSchemaConfiguration {

/**
* @return A value that should match the JSON schema $id attribute.
*/
String schemaId();

/**
* Allows forcing the java type qualified name of the schema. If the type already exists, it will
* be used as is.
*
* <p>E.g. "com.aqme.Foo"
*/
String javaTypeQualifiedName() default "";

/**
* @return The interfaces that the generated record must implement.
*/
String[] javaInterfaceQualifiedNames() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.cosium.json_schema_to_java_record_api;

/**
* @author Réda Housni Alaoui
*/
public @interface JsonSchemaFileLocation {
/**
* e.g. "com.aqme"
*
* <p>Use empty string "" for the root package
*/
String moduleAndPackage();

/** e.g. "foo.schema.json" */
String relativeName();
}
31 changes: 31 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.cosium.json_schema_to_java_record</groupId>
<artifactId>json-schema-to-java-record-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<name>JSON schema to Java Record BOM</name>
<artifactId>json-schema-to-java-record-bom</artifactId>

<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>json-schema-to-java-record-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>json-schema-to-java-record</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
48 changes: 48 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.cosium.json_schema_to_java_record</groupId>
<artifactId>json-schema-to-java-record-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<name>JSON schema to Java Record</name>
<artifactId>json-schema-to-java-record</artifactId>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>json-schema-to-java-record-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.palantir.javapoet</groupId>
<artifactId>javapoet</artifactId>
</dependency>

<dependency>
<groupId>io.github.zenwave360</groupId>
<artifactId>json-schema-ref-parser-jvm</artifactId>
</dependency>

<dependency>
<groupId>com.cosium.logging</groupId>
<artifactId>annotation-processor-logger</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.cosium.json_schema_to_java_record;

import static java.util.Objects.requireNonNull;

import io.zenwave360.jsonrefparser.$Ref;
import io.zenwave360.jsonrefparser.resolver.Resolver;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;

/**
* @author Réda Housni Alaoui
*/
class $RefClasspathResolver implements Resolver {

private final ReadonlyClassPathResources classPathResources;

$RefClasspathResolver(ReadonlyClassPathResources classPathResources) {
this.classPathResources = requireNonNull(classPathResources);
}

@Override
public String resolve($Ref $ref) {
try {
URI uri = $ref.getURI();
if (uri.toString().startsWith("classpath:") && !uri.toString().startsWith("classpath:/")) {
// gracefully handle classpath: without the slash
uri = URI.create(uri.toString().replace("classpath:", "classpath:/"));
}

String uriPath = uri.getPath().replaceFirst("^/", "");
int lastIndexOfSlash = uriPath.lastIndexOf("/");

String packageName = uriPath.substring(0, lastIndexOfSlash).replace("/", ".");
String relativeName = uriPath.substring(lastIndexOfSlash + 1);
try (InputStream inputStream =
classPathResources.openInputStream(packageName, relativeName)) {
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}

} catch (MissingResourceException missingResourceException) {
throw missingResourceException;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Loading