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