指定API标签后Gradle OpenAPI插件未生成模型求助
问题:指定API标签筛选时模型未生成
我尝试通过Gradle的OpenAPI插件,基于以下OpenAPI 3.0.3规范生成Java代码:
openapi: "3.0.3" info: title: Demo API version: "1.0" servers: - url: http://localhost:8080/api description: Local development server tags: - name: Common description: Operations related to common functionalities. Define multiple tags to generate multiple Api classes. - name: Other description: To test generation of separate APIs paths: /UM/{id}: get: tags: - Common description: Retrieve UM by ID operationId: GetUM parameters: - in: path name: id schema: type: string required: true responses: "200": description: UM content: application/json: schema: { } "404": description: UM not found content: text/plain: schema: type: string /UM: post: tags: - Common description: Create new UM operationId: CreateUM requestBody: required: true content: application/json: schema: { } responses: 201: description: ID of the newly created UM content: text/plain: schema: type: string /palettes/{id}: get: tags: - Common operationId: getPalette # necessary to generate proper method name description: Retrieve a palette by its id parameters: - in: path name: id schema: type: integer required: true responses: "200": description: OK content: application/json: schema: $ref: "#/components/schemas/Palette" examples: Default: value: id: 1 firstName: John lastName: Doe role: user "404": description: Palette not found content: text/plain: schema: type: string /palettes: get: tags: - Common operationId: listPalettes description: Returns the list of palettes responses: "200": description: OK content: application/json: schema: type: array items: $ref: "#/components/schemas/Palette" put: tags: - Common operationId: createPalette # necessary to generate proper method name description: Creates a new palette requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/NewPalette" responses: "201": description: Created content: text/plain: schema: type: integer "400": description: Validation errors content: application/json: schema: $ref: "#/components/schemas/ValidationError" /orders: put: tags: - Common operationId: sendOrder description: Sends a dummy order responses: "200": description: OK content: text/plain: schema: type: string /otherresources: get: tags: - Other operationId: getOther # necessary to generate proper method name description: Example operation responses: "200": description: OK content: application/json: schema: $ref: "#/components/schemas/Palette" components: schemas: ObjectType: type: string enum: [ ENGINE, SEAT ] Palette: type: object x-tags: - Common properties: id: type: integer description: The user ID firstName: type: string description: The user's first name lastName: type: string description: The palette's last name minLength: 1 maxLength: 20 role: $ref: "#/components/schemas/ObjectType" NewPalette: type: object properties: firstName: type: string description: The user's first name lastName: type: string description: The user's last name minLength: 1 maxLength: 20 role: $ref: "#/components/schemas/ObjectType" ValidationErrorField: properties: field: type: string message: type: string constraint: type: string value: type: string example: - field: name message: size must be between 1 and 20 constraint: Size value: This is way toooooooo long a name! ValidationError: properties: status: type: string message: type: string errors: type: array $ref: "#/components/schemas/ValidationErrorField" example: - status: Bad Request message: Validation failed errors: - field: lastName message: size must be between 1 and 20 constraint: Size value: This is wayyyyyy toooo loooooong securitySchemes: oidc: type: oauth2 flows: authorizationCode: authorizationUrl: http://localhost:8180/realms/quarkus/protocol/openid-connect/auth tokenUrl: http://localhost:8180/realms/quarkus/protocol/openid-connect/token refreshUrl: http://localhost:8180/realms/quarkus/protocol/openid-connect/token scopes: openid: OpenID Connect authentication security: - oidc: [ ]
使用的Gradle任务配置如下:
openApiGenerate { generatorName = 'jaxrs-spec' inputSpec = file(openapiSourcefile).absolutePath outputDir = file(openapiGeneratedSources).absolutePath apiPackage = "${apiPackageName}.controller" modelPackage = "${apiPackageName}.model" generateAliasAsModel = true verbose = true globalProperties = [ 'apis' : project.ext.apisToGenerate, 'models': 'true' ] configOptions = [ interfaceOnly : 'true', singleContentTypes : 'true', useSingleRequestMethod : 'true', useSwaggerAnnotations : 'false', useTags : 'true', dateLibrary : 'java8', library : 'quarkus', useMicroProfileOpenAPIAnnotations : 'true', additionalModelTypeAnnotations : '@jakarta.validation.constraints.NotNull', useBeanValidation : 'true', useJakartaEe : 'true', disallowAdditionalPropertiesIfNotPresent: 'false', generateModelTests : 'false', generateModelDocumentation : 'false', generateApiTests : 'false', generateApiDocumentation : 'false', generateBuilders : 'true', modelPropertyNaming : 'original', returnResponse : 'true', ] }
其中project.ext.apisToGenerate设置为'Common'。遇到的问题是:指定API标签筛选后,模型未被生成;只有移除apis筛选配置时,模型才正常生成。但我需要仅生成指定标签的API,请问是否需要在models属性中显式列出需要生成的模型?
解决方案
不需要显式列出所有模型,问题出在OpenAPI Generator的模型关联逻辑:当通过apis参数筛选指定标签的API时,默认只会生成这些API直接引用的模型,但可以通过以下方式调整配置解决:
利用模型的
x-tags筛选
你的规范中已经给Palette模型添加了x-tags: [Common],将globalProperties里的models参数设置为'Common',插件会自动生成所有带有Common标签的模型,以及这些模型依赖的关联模型(比如ObjectType、NewPalette等):globalProperties = [ 'apis' : project.ext.apisToGenerate, // 值为'Common' 'models': 'Common' ]强制生成所有依赖模型
如果不想按标签筛选模型,可将models设置为'*',插件会生成所有被筛选后的API引用到的模型,不会遗漏依赖:globalProperties = [ 'apis' : project.ext.apisToGenerate, 'models': '*' ]验证插件版本
确保使用的OpenAPI Generator Gradle插件是较新版本,旧版本可能存在筛选API时模型关联失效的bug,建议升级到最新稳定版。
内容的提问来源于stack exchange,提问作者Franck Dervaux
相关产品推荐
相关产品推荐

