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.plugins.annotations.LifecyclePhase;
020import org.apache.maven.plugins.annotations.Mojo;
021import org.apache.maven.plugins.annotations.Parameter;
022
023import java.util.List;
024
025/**
026 * Generates an OpenAPI client.
027 * The sources are generated in the target directory.
028 */
029@Mojo(name = OpenApiClientMojo.MOJO_NAME, defaultPhase = LifecyclePhase.GENERATE_SOURCES)
030public class OpenApiClientMojo extends AbstractOpenApiMojo {
031
032    public static final String MOJO_NAME = "generate-openapi-client";
033
034    private static final String CLIENT_PREFIX = MICRONAUT_OPENAPI_PREFIX + ".client.";
035
036    /**
037     * Client id.
038     */
039    @Parameter(property = CLIENT_PREFIX + "id")
040    protected String clientId;
041
042    /**
043     * If set to true, Api annotation {@literal @}Client will be with `path` attribute.
044     */
045    @Parameter(property = CLIENT_PREFIX + "path")
046    protected boolean clientPath;
047
048    /**
049     * Whether to configure authentication for client.
050     */
051    @Parameter(property = CLIENT_PREFIX + "use.auth", defaultValue = "false")
052    protected boolean useAuth;
053
054    /**
055     * Additional annotations to be used on the generated client API classes.
056     */
057    @Parameter(property = CLIENT_PREFIX + "additional.type.annotations")
058    protected List<String> additionalTypeAnnotations;
059
060    /**
061     * The base path separator.
062     */
063    @Parameter(property = CLIENT_PREFIX + "base.path.separator")
064    protected String basePathSeparator;
065
066    /**
067     * The pattern for authorization filter.
068     */
069    @Parameter(property = CLIENT_PREFIX + "authorization.filter.pattern")
070    protected String authorizationFilterPattern;
071
072    /**
073     * The property that defines if this mojo is used.
074     */
075    @Parameter(property = MICRONAUT_OPENAPI_PREFIX + ".generate.client")
076    protected boolean enabled;
077
078    /**
079     * Determines if the client should use lombok.
080     *
081     * @since 4.2.2
082     */
083    @Parameter(property = CLIENT_PREFIX + "lombok")
084    protected boolean lombok;
085
086    /**
087     * Determines if the client should use flux for arrays.
088     *
089     * @since 4.2.2
090     */
091    @Parameter(property = CLIENT_PREFIX + "flux.for.arrays")
092    protected boolean fluxForArrays;
093
094    /**
095     * If set to true, the `javax.annotation.Generated` annotation will be added to all generated classes.
096     *
097     * @since 4.2.2
098     */
099    @Parameter(property = CLIENT_PREFIX + "generated.annotation", defaultValue = "true")
100    protected boolean generatedAnnotation;
101
102    @Override
103    protected boolean isEnabled() {
104        return enabled;
105    }
106
107    @Override
108    protected void configureBuilder(MicronautCodeGeneratorBuilder builder) {
109        if ("kotlin".equalsIgnoreCase(lang)) {
110            builder.forKotlinClient(spec -> {
111                spec.withAuthorization(useAuth)
112                    .withGeneratedAnnotation(generatedAnnotation)
113                    .withFluxForArrays(fluxForArrays)
114                    .withKsp(ksp)
115                    .withClientPath(clientPath);
116
117                if (clientId != null && !clientId.isEmpty()) {
118                    spec.withClientId(clientId);
119                }
120                if (additionalTypeAnnotations != null) {
121                    spec.withAdditionalClientTypeAnnotations(additionalTypeAnnotations);
122                }
123                if (basePathSeparator != null) {
124                    spec.withBasePathSeparator(basePathSeparator);
125                }
126                if (authorizationFilterPattern != null) {
127                    spec.withAuthorizationFilterPattern(authorizationFilterPattern);
128                }
129            });
130        } else if ("java".equalsIgnoreCase(lang)) {
131            builder.forJavaClient(spec -> {
132                spec.withAuthorization(useAuth)
133                    .withLombok(lombok)
134                    .withGeneratedAnnotation(generatedAnnotation)
135                    .withFluxForArrays(fluxForArrays)
136                    .withClientPath(clientPath);
137
138                if (clientId != null && !clientId.isEmpty()) {
139                    spec.withClientId(clientId);
140                }
141                if (additionalTypeAnnotations != null) {
142                    spec.withAdditionalClientTypeAnnotations(additionalTypeAnnotations);
143                }
144                if (basePathSeparator != null) {
145                    spec.withBasePathSeparator(basePathSeparator);
146                }
147                if (authorizationFilterPattern != null) {
148                    spec.withAuthorizationFilterPattern(authorizationFilterPattern);
149                }
150            });
151        } else {
152            throw new UnsupportedOperationException("Unsupported language:" + lang);
153        }
154    }
155}