generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an initial version of the JSON schema processors
- Loading branch information
1 parent
d438db9
commit 2008eb5
Showing
62 changed files
with
3,331 additions
and
39 deletions.
There are no files selected for viewing
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
buildSrc/src/main/groovy/io.micronaut.build.internal.json-schema-module.gradle
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
plugins { | ||
id 'io.micronaut.build.internal.json-schema-base' | ||
id "io.micronaut.build.internal.module" | ||
} | ||
|
||
micronautBuild { | ||
binaryCompatibility { | ||
enabled.set(false) | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
buildSrc/src/main/groovy/io.micronaut.build.internal.project-template-module.gradle
This file was deleted.
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
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,10 +1,10 @@ | ||
projectVersion=1.0.0-SNAPSHOT | ||
projectGroup=io.micronaut.project-template | ||
projectGroup=io.micronaut.jsonschema | ||
|
||
title=Micronaut project-template | ||
projectDesc=TODO | ||
title=Micronaut JSON schema | ||
projectDesc=JSON schema support for Micronaut | ||
projectUrl=https://micronaut.io | ||
githubSlug=micronaut-projects/micronaut-project-template | ||
developers=Graeme Rocher | ||
githubSlug=micronaut-projects/micronaut-json-schema | ||
developers=Andriy Dmytruk | ||
org.gradle.caching=true | ||
org.gradle.jvmargs=-Xmx1g |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
plugins { | ||
id 'io.micronaut.build.internal.json-schema-module' | ||
} | ||
|
||
dependencies { | ||
|
||
} | ||
|
||
configurations.configureEach { | ||
all*.exclude group: "ch.qos.logback" | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
|
||
maxHeapSize = "1024m" | ||
} |
51 changes: 51 additions & 0 deletions
51
json-schema-annotations/src/main/java/io/micronaut/jsonschema/JsonSchema.java
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed 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. | ||
*/ | ||
package io.micronaut.jsonschema; | ||
|
||
/** | ||
* An annotation that signifies that json schema should be created for the object. | ||
* The JSON schema will attempt to mimic the way this object would be serialized. | ||
* | ||
* @since 1.0.0 | ||
* @author Andriy Dmytruk | ||
*/ | ||
public @interface JsonSchema { | ||
|
||
/** | ||
* The title of the JSON schema. | ||
* By default, the class name will be used. | ||
* | ||
* @return The title | ||
*/ | ||
String title() default ""; | ||
|
||
/** | ||
* The description of the JSON schema. | ||
* By default, javadoc of the object will be used. | ||
* | ||
* @return The description | ||
*/ | ||
String description() default ""; | ||
|
||
/** | ||
* The schema's relative or absolute URI. | ||
* The default will create the URI based on class name and configured base URI. | ||
* | ||
* @return The URI | ||
*/ | ||
String uri() default ""; | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
json-schema-annotations/src/main/java/io/micronaut/jsonschema/JsonSchemaConfiguration.java
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed 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. | ||
*/ | ||
package io.micronaut.jsonschema; | ||
|
||
/** | ||
* An annotation for globally configuring the JSON schema generation. | ||
* | ||
* @since 1.0.0 | ||
* @author Andriy Dmytruk | ||
*/ | ||
public @interface JsonSchemaConfiguration { | ||
|
||
/** | ||
* The location where JSON schemas will be generated inside the build {@code META-INF/} directory. | ||
* | ||
* @return The output location | ||
*/ | ||
String outputLocation() default "schemas"; | ||
|
||
/** | ||
* The base URI to be used for schemas. | ||
* | ||
* @return The base URI | ||
*/ | ||
String baseUri(); | ||
|
||
/** | ||
* Whether to encode byte array as a JSON array. | ||
* The default and preferred behavior is to encode it as a Base64 string. | ||
* | ||
* @return Whether to represent binary data as array | ||
*/ | ||
boolean binaryAsArray() default false; | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
plugins { | ||
id 'io.micronaut.build.internal.json-schema-base' | ||
id "io.micronaut.build.internal.bom" | ||
} | ||
|
||
micronautBuild { | ||
binaryCompatibility { | ||
enabled.set(false) | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
plugins { | ||
id 'io.micronaut.build.internal.json-schema-module' | ||
} | ||
|
||
dependencies { | ||
compileOnly(mn.micronaut.core.processor) | ||
|
||
implementation(mn.micronaut.http) | ||
|
||
api(projects.micronautJsonSchemaAnnotations) | ||
api(mn.jackson.databind) | ||
|
||
testImplementation(mnValidation.validation) | ||
testImplementation(mn.micronaut.inject.kotlin.test) | ||
testImplementation(mn.micronaut.inject.groovy.test) | ||
testImplementation(mn.micronaut.inject.java.test) | ||
testImplementation(mnLogging.logback.classic) | ||
testImplementation(mnLogging.logback.core) | ||
testImplementation(mnLogging.slf4j.api) | ||
testImplementation(mnLogging.slf4j.simple) | ||
} | ||
|
||
configurations.configureEach { | ||
all*.exclude group: "ch.qos.logback" | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
|
||
maxHeapSize = "1024m" | ||
} |
93 changes: 93 additions & 0 deletions
93
...ocessor/src/main/java/io/micronaut/jsonschema/visitor/JsonSchemaConfigurationVisitor.java
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed 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. | ||
*/ | ||
package io.micronaut.jsonschema.visitor; | ||
|
||
import io.micronaut.core.annotation.AnnotationValue; | ||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.inject.ast.ClassElement; | ||
import io.micronaut.inject.visitor.TypeElementVisitor; | ||
import io.micronaut.inject.visitor.VisitorContext; | ||
import io.micronaut.jsonschema.JsonSchemaConfiguration; | ||
import io.micronaut.jsonschema.visitor.model.Schema; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* A visitor for reading the JSON schema configuration. | ||
* It must be defined with a {@link JsonSchemaConfiguration} annotation on a bean. | ||
* | ||
* @since 1.0.0 | ||
* @author Andriy Dmytruk | ||
*/ | ||
@Internal | ||
public final class JsonSchemaConfigurationVisitor implements TypeElementVisitor<JsonSchemaConfiguration, Object> { | ||
|
||
public static final String JSON_SCHEMA_CONFIGURATION_PROPERTY = "io.micronaut.jsonschema.config"; | ||
|
||
@Override | ||
public int getOrder() { | ||
return 1; // Run before the JSON Schema visitor | ||
} | ||
|
||
@Override | ||
public @NonNull TypeElementVisitor.VisitorKind getVisitorKind() { | ||
return VisitorKind.AGGREGATING; | ||
} | ||
|
||
@Override | ||
public void visitClass(ClassElement element, VisitorContext visitorContext) { | ||
AnnotationValue<?> annotation = element.getAnnotation(JsonSchemaConfiguration.class); | ||
if (annotation != null) { | ||
String outputLocation = annotation.stringValue("outputLocation") | ||
.orElse(JsonSchemaContext.DEFAULT_OUTPUT_LOCATION); | ||
String baseUri = annotation.getRequiredValue("baseUri", String.class); | ||
if (baseUri.endsWith("/")) { | ||
baseUri = baseUri.substring(0, baseUri.length() - 1); | ||
} | ||
boolean binaryAsArray = annotation.booleanValue("binaryAsArray") | ||
.orElse(JsonSchemaContext.DEFAULT_BINARY_AS_ARRAY); | ||
JsonSchemaContext context = new JsonSchemaContext(outputLocation, baseUri, binaryAsArray, new HashMap<>()); | ||
visitorContext.put(JSON_SCHEMA_CONFIGURATION_PROPERTY, context); | ||
} | ||
} | ||
|
||
/** | ||
* A configuration for the JSON schema. | ||
* | ||
* @param outputLocation The output location for schemas | ||
* @param baseUrl The base URL of the schemas | ||
* @param binaryAsArray Whether to represent byte arrays as arrays instead of base 64 string | ||
* @param createdSchemasByType A cache of crated schemas | ||
*/ | ||
public record JsonSchemaContext( | ||
String outputLocation, | ||
String baseUrl, | ||
boolean binaryAsArray, | ||
Map<String, Schema> createdSchemasByType | ||
) { | ||
public static final String DEFAULT_OUTPUT_LOCATION = "schemas"; | ||
public static final boolean DEFAULT_BINARY_AS_ARRAY = false; | ||
|
||
public static JsonSchemaContext createDefault() { | ||
return new JsonSchemaContext( | ||
DEFAULT_OUTPUT_LOCATION, null, DEFAULT_BINARY_AS_ARRAY, new HashMap<>() | ||
); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.