001/*
002 * Copyright 2017-2021 original authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.micronaut.maven.openapi;
017
018import io.micronaut.openapi.generator.MicronautCodeGeneratorBuilder;
019import org.apache.maven.plugin.MojoExecutionException;
020import org.apache.maven.plugins.annotations.LifecyclePhase;
021import org.apache.maven.plugins.annotations.Mojo;
022import org.apache.maven.plugins.annotations.Parameter;
023
024/**
025 * Generates an OpenAPI server.
026 * The sources are generated in the target directory.
027 */
028@Mojo(name = OpenApiServerMojo.MOJO_NAME, defaultPhase = LifecyclePhase.GENERATE_SOURCES)
029public class OpenApiServerMojo extends AbstractOpenApiMojo {
030
031    public static final String MOJO_NAME = "generate-openapi-server";
032
033    private static final String SERVER_PREFIX = MICRONAUT_OPENAPI_PREFIX + ".server.";
034
035    /**
036     * The package name of the controller if controller implementation files are generated.
037     */
038    @Parameter(property = SERVER_PREFIX + "controller.package.name", defaultValue = IO_MICRONAUT_OPENAPI_PREFIX + ".controller.package.name", required = true)
039    protected String controllerPackageName;
040
041    /**
042     * Whether to generate authentication annotations for APIs.
043     */
044    @Parameter(property = SERVER_PREFIX + "use.auth", defaultValue = "false")
045    protected boolean useAuth;
046
047    /**
048     * Determines if the server should use lombok.
049     *
050     * @since 4.2.2
051     */
052    @Parameter(property = SERVER_PREFIX + "lombok")
053    protected boolean lombok;
054
055    /**
056     * Determines if the server should use flux for arrays.
057     *
058     * @since 4.2.2
059     */
060    @Parameter(property = SERVER_PREFIX + "flux.for.arrays")
061    protected boolean fluxForArrays;
062
063    /**
064     * If set to true, the `javax.annotation.Generated` annotation will be added to all generated classes.
065     *
066     * @since 4.2.2
067     */
068    @Parameter(property = SERVER_PREFIX + "generated.annotation", defaultValue = "true")
069    protected boolean generatedAnnotation;
070
071    /**
072     * If set to true, the generated code should be made compatible with Micronaut AOT.
073     *
074     * @since 4.2.2
075     */
076    @Parameter(property = SERVER_PREFIX + "aot.compatible")
077    protected boolean aotCompatible;
078
079    /**
080     * The property that defines if this mojo should be used in configuration.
081     */
082    @Parameter(property = MICRONAUT_OPENAPI_PREFIX + ".generate.server")
083    protected boolean enabled;
084
085    @Override
086    protected void validateLanguage() throws MojoExecutionException {
087        rejectUnsupportedKotlinLanguage("server");
088    }
089
090    @Override
091    protected boolean isEnabled() {
092        return enabled;
093    }
094
095    @Override
096    protected void configureBuilder(MicronautCodeGeneratorBuilder builder) {
097        if ("java".equalsIgnoreCase(lang)) {
098            builder.forJavaServer(spec -> spec
099                .withControllerPackage(controllerPackageName)
100                .withAuthentication(useAuth)
101                .withAot(aotCompatible)
102                // we don't want these to be configurable in the plugin for now
103                .withGenerateImplementationFiles(false)
104                .withGenerateControllerFromExamples(false)
105                .withGenerateOperationsToReturnNotImplemented(false)
106                .withGeneratedAnnotation(generatedAnnotation)
107                .withFluxForArrays(fluxForArrays)
108                .withLombok(lombok)
109            );
110        } else {
111            throw new UnsupportedOperationException("Unsupported language: " + lang);
112        }
113    }
114}