Skip to content
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

Add micronaut-json-schema feature. #2432

Merged
merged 9 commits into from
Jun 1, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class MicronautDependencyUtils {
public static final String GROUP_ID_MICRONAUT_CRAC = "io.micronaut.crac";
public static final String GROUP_ID_MICRONAUT_ECLIPSESTORE = "io.micronaut.eclipsestore";
public static final String GROUP_ID_MICRONAUT_GCP = "io.micronaut.gcp";
public static final String GROUP_ID_MICRONAUT_JSON_SCHEMA = "io.micronaut.json-schema";
public static final String GROUP_ID_MICRONAUT_KAFKA = "io.micronaut.kafka";
public static final String GROUP_ID_MICRONAUT_OCI = "io.micronaut.oraclecloud";
public static final String GROUP_ID_MICRONAUT_OPENSEARCH = "io.micronaut.opensearch";
Expand Down Expand Up @@ -210,6 +211,11 @@ public static Dependency.Builder gcpDependency() {
return micronautDependency(GROUP_ID_MICRONAUT_GCP);
}

@NonNull
public static Dependency.Builder jsonSchemaDependency() {
return micronautDependency(GROUP_ID_MICRONAUT_JSON_SCHEMA);
}

@NonNull
public static Dependency.Builder kafkaDependency() {
return micronautDependency(GROUP_ID_MICRONAUT_KAFKA);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.starter.feature.json;

import io.micronaut.starter.application.ApplicationType;
import io.micronaut.starter.application.generator.GeneratorContext;
import io.micronaut.starter.build.dependencies.Dependency;
import io.micronaut.starter.build.dependencies.MicronautDependencyUtils;
import io.micronaut.starter.feature.Category;
import io.micronaut.starter.feature.staticResources.ContributingStaticResources;
import io.micronaut.starter.feature.staticResources.StaticResource;
import jakarta.inject.Singleton;

import java.util.Collections;
import java.util.List;

@Singleton
public class JsonSchemaFeature implements ContributingStaticResources {

public static final String NAME = "json-schema";

private static final Dependency JSON_SCHEMA_PROCESSOR_DEPENDENCY = MicronautDependencyUtils.jsonSchemaDependency()
.artifactId("micronaut-json-schema-processor")
.annotationProcessor()
.build();

private static final Dependency JSON_SCHEMA_ANNOTAIONS_DEPENDENCY = MicronautDependencyUtils.jsonSchemaDependency()
.artifactId("micronaut-json-schema-annotations")
.compile()
.build();

@Override
public String getName() {
return NAME;
}

@Override
public String getTitle() {
return "JSON Schema";
}

@Override
public String getDescription() {
return "Adds JSON Schema to a Micronaut Application";
}

@Override
public boolean supports(ApplicationType applicationType) {
return true;
}

@Override
public String getCategory() {
return Category.API;
}

@Override
public void apply(GeneratorContext generatorContext) {
generatorContext.addDependency(JSON_SCHEMA_PROCESSOR_DEPENDENCY);
generatorContext.addDependency(JSON_SCHEMA_ANNOTAIONS_DEPENDENCY);
}

@Override
public String getThirdPartyDocumentation() {
return "https://json-schema.org/learn/getting-started-step-by-step";
}

@Override
public String getMicronautDocumentation() {
return "https://micronaut-projects.github.io/micronaut-json-schema/latest/guide/";
}

@Override
public List<StaticResource> staticResources() {
return Collections.singletonList(new StaticResource("jsonschema", "/schemas/**", "classpath:META-INF/schemas"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.starter.feature.json;

import io.micronaut.starter.application.ApplicationType;
import io.micronaut.starter.application.generator.GeneratorContext;
import io.micronaut.starter.build.dependencies.Dependency;
import io.micronaut.starter.build.dependencies.MicronautDependencyUtils;
import io.micronaut.starter.feature.Category;
import io.micronaut.starter.feature.Feature;
import io.micronaut.starter.feature.FeatureContext;
import jakarta.inject.Singleton;

@Singleton
public class JsonSchemaValidationFeature implements Feature {
wetted marked this conversation as resolved.
Show resolved Hide resolved

public static final String NAME = "json-schema-validation";

private static final Dependency JSON_SCHEMA_VALIDATION_TEST_DEPENDENCY = MicronautDependencyUtils.jsonSchemaDependency()
.artifactId("micronaut-json-schema-validation")
.test()
.build();

private final JsonSchemaFeature jsonSchemaFeature;

public JsonSchemaValidationFeature(JsonSchemaFeature jsonSchemaFeature) {
this.jsonSchemaFeature = jsonSchemaFeature;
}

@Override
public String getName() {
return NAME;
}

@Override
public String getTitle() {
return "JSON Schema Validation";
}

@Override
public String getDescription() {
return "Adds JSON Schema Validation to a Micronaut Application";
}

@Override
public boolean supports(ApplicationType applicationType) {
return true;
}

@Override
public String getCategory() {
return Category.API;
}

@Override
public void apply(GeneratorContext generatorContext) {
generatorContext.addDependency(JSON_SCHEMA_VALIDATION_TEST_DEPENDENCY);
}

@Override
public void processSelectedFeatures(FeatureContext featureContext) {
featureContext.addFeatureIfNotPresent(JsonSchemaFeature.class, jsonSchemaFeature);
}

@Override
public String getMicronautDocumentation() {
return "https://micronaut-projects.github.io/micronaut-json-schema/latest/guide/index.html#validation";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import io.micronaut.starter.feature.aws.Cdk
import io.micronaut.starter.feature.database.HibernateReactiveFeature
import io.micronaut.starter.feature.database.JAsyncSQLFeature
import io.micronaut.starter.feature.function.awslambda.AwsLambda
import io.micronaut.starter.feature.json.JsonSchemaFeature
import io.micronaut.starter.feature.json.JsonSchemaValidationFeature
import io.micronaut.starter.feature.lang.groovy.module.GroovyModuleFeature
import io.micronaut.starter.options.*
import spock.lang.Unroll
Expand Down Expand Up @@ -64,6 +66,9 @@ class FeatureSpec extends BeanContextSpec {
} else if (feature instanceof Cdk || feature instanceof AwsLambdaEventFunctionFeature) {
// Cdk fails unless it is combined with Lambda
features << AwsLambda.FEATURE_NAME_AWS_LAMBDA
} else if (feature instanceof JsonSchemaValidationFeature) {
// fails unless it is combined with JsonSchemaFeature
features << JsonSchemaFeature.NAME
}
ApplicationType applicationType = applicationTypeForFeature(feature)
def commandCtx = new GeneratorContext(buildProject(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.micronaut.starter.feature.json

import io.micronaut.starter.ApplicationContextSpec
import io.micronaut.starter.BuildBuilder
import io.micronaut.starter.application.ApplicationType
import io.micronaut.starter.application.generator.GeneratorContext
import io.micronaut.starter.build.BuildTestUtil
import io.micronaut.starter.build.BuildTestVerifier
import io.micronaut.starter.feature.Category
import io.micronaut.starter.fixture.CommandOutputFixture
import io.micronaut.starter.options.BuildTool
import io.micronaut.starter.options.Language
import spock.lang.Shared
import spock.lang.Subject

class JsonSchemaFeatureSpec extends ApplicationContextSpec implements CommandOutputFixture {

@Shared
@Subject
JsonSchemaFeature jsonSchemaFeature = beanContext.getBean(JsonSchemaFeature)

void 'readme.md with feature json-schema contains links to docs'() {
when:
Map<String, String> output = generate([JsonSchemaFeature.NAME])
String readme = output["README.md"]

then:
readme
readme.contains("https://micronaut-projects.github.io/micronaut-json-schema/latest/guide/")
readme.contains("https://json-schema.org/learn/getting-started-step-by-step")
}

void 'test json-schema configuration'() {
when:
GeneratorContext commandContext = buildGeneratorContext([JsonSchemaFeature.NAME])

then:
commandContext.configuration.get('micronaut.router.static-resources.jsonschema.paths'.toString()) == 'classpath:META-INF/schemas'
commandContext.configuration.get('micronaut.router.static-resources.jsonschema.mapping'.toString()) == '/schemas/**'
}


void "json-schema belongs to API category"() {
expect:
Category.API == jsonSchemaFeature.category
}

void "json-schema supports application type = #applicationType"(ApplicationType applicationType) {
expect:
jsonSchemaFeature.supports(applicationType)

where:
applicationType << ApplicationType.values()
}

void "json-schema feature adds dependencies for language=#language buildTool=#buildTool "(BuildTool buildTool, Language language) {
when:
String template = new BuildBuilder(beanContext, buildTool)
.features([JsonSchemaFeature.NAME])
.language(language)
.render()
BuildTestVerifier verifier = BuildTestUtil.verifier(buildTool, language, template)

then:
template
verifier.hasAnnotationProcessor("io.micronaut.json-schema", "micronaut-json-schema-processor")
verifier.hasDependency("io.micronaut.json-schema", "micronaut-json-schema-annotations")

where:
[buildTool, language] << [BuildTool.values(), Language.values()].combinations()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.micronaut.starter.feature.json

import io.micronaut.starter.ApplicationContextSpec
import io.micronaut.starter.BuildBuilder
import io.micronaut.starter.application.ApplicationType
import io.micronaut.starter.application.generator.GeneratorContext
import io.micronaut.starter.build.BuildTestUtil
import io.micronaut.starter.build.BuildTestVerifier
import io.micronaut.starter.build.dependencies.Scope
import io.micronaut.starter.feature.Category
import io.micronaut.starter.fixture.CommandOutputFixture
import io.micronaut.starter.options.BuildTool
import io.micronaut.starter.options.Language
import spock.lang.Shared
import spock.lang.Subject

class JsonSchemaValidationFeatureSpec extends ApplicationContextSpec implements CommandOutputFixture {

@Shared
@Subject
JsonSchemaValidationFeature jsonSchemaValidationFeature = beanContext.getBean(JsonSchemaValidationFeature)

void 'readme.md with feature json-schema-validation contains links to docs'() {
when:
Map<String, String> output = generate([JsonSchemaFeature.NAME, JsonSchemaValidationFeature.NAME])
String readme = output["README.md"]

then:
readme
readme.contains("https://micronaut-projects.github.io/micronaut-json-schema/latest/guide/index.html#validation")
}

void "json-schema-validation belongs to API category"() {
expect:
Category.API == jsonSchemaValidationFeature.category
}

void "json-schema-validation supports application type = #applicationType"(ApplicationType applicationType) {
expect:
jsonSchemaValidationFeature.supports(applicationType)

where:
applicationType << ApplicationType.values()
}

void "json-schema-validation feature adds test dependency for language=#language buildTool=#buildTool "(BuildTool buildTool, Language language) {
when:
String template = new BuildBuilder(beanContext, buildTool)
.features([JsonSchemaFeature.NAME, JsonSchemaValidationFeature.NAME])
.language(language)
.render()
BuildTestVerifier verifier = BuildTestUtil.verifier(buildTool, language, template)

then:
template
verifier.hasDependency("io.micronaut.json-schema", "micronaut-json-schema-validation", Scope.TEST)

where:
[buildTool, language] << [BuildTool.values(), Language.values()].combinations()
}

void 'test json-schema-validation feature adds jason-schema feature'() {
when:
GeneratorContext generatorContext = buildGeneratorContext([JsonSchemaValidationFeature.NAME])

then:
generatorContext.hasFeature(JsonSchemaFeature)
}
}
Loading