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,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 jakarta.inject.Singleton;

@Singleton
public class JsonSchemaFeature implements Feature {

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/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import groovy.transform.CompileStatic
import io.swagger.v3.oas.annotations.*
import io.swagger.v3.oas.annotations.info.*
}
@if (features.contains("json-schema")) {
import io.micronaut.jsonschema.JsonSchemaConfiguration
}


@if (features.contains("openapi")) {
Expand All @@ -28,6 +31,11 @@ import io.swagger.v3.oas.annotations.info.*
)
)
}
@if (features.contains("json-schema")) {
@@JsonSchemaConfiguration(
baseUri = "http://localhost:8080/schemas"
)
}
@@CompileStatic
class Application {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ import io.dekorate.jaeger.annotation.EnableJaegerAgent;
@if (features.contains("dekorate-halkyon")) {
import io.dekorate.halkyon.annotation.HalkyonComponent;
}
@if (features.contains("json-schema")) {
import io.micronaut.jsonschema.JsonSchemaConfiguration;
}


@if (features.contains("openapi")) {
Expand All @@ -54,6 +57,11 @@ import io.dekorate.halkyon.annotation.HalkyonComponent;
)
)
}
@if (features.contains("json-schema")) {
@@JsonSchemaConfiguration(
baseUri = "http://localhost:8080/schemas"
)
}

@for(Feature feature : features.getFeatures().stream().filter(f -> f instanceof AbstractDekoratePlatformFeature).iterator()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import io.dekorate.jaeger.annotation.EnableJaegerAgent
@if (features.contains("dekorate-halkyon")) {
import io.dekorate.halkyon.annotation.HalkyonComponent
}
@if (features.contains("json-schema")) {
import io.micronaut.jsonschema.JsonSchemaConfiguration
}

@if (features.contains("openapi")) {
import io.swagger.v3.oas.annotations.*
Expand All @@ -53,6 +56,11 @@ import io.swagger.v3.oas.annotations.info.*
object Api {
}
}
@if (features.contains("json-schema")) {
@@JsonSchemaConfiguration(
baseUri = "http://localhost:8080/schemas"
)
}

@if (features.stream().anyMatch((s) -> s.startsWith("dekorate"))){
@for(Feature feature : features.getFeatures().stream().filter(f -> f instanceof AbstractDekoratePlatformFeature).iterator()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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.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 io.micronaut.starter.options.Options
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 "json-schema belongs to Validation category"() {
expect:
Category.API == jsonSchemaFeature.category
}

void 'test application class has default annotation for language=#language'(Language language) {
when:
Options options = new Options(language)
Map<String, String> output = generate(ApplicationType.DEFAULT, options, [JsonSchemaFeature.NAME])
String appSource = output[language.getSourcePath("/example/micronaut/Application")]

then:
appSource
appSource.contains("import io.micronaut.jsonschema.JsonSchemaConfiguration")
appSource.contains("""
@JsonSchemaConfiguration(
baseUri = "http://localhost:8080/schemas"
)
""")

where:
language << Language.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()
}
}
Loading