Skip to content

[BUG] aspcorenet --import-mappings is not working #21094

@vkotu

Description

@vkotu

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using the aspnetcore generator with both typeMappings and importMappings in the openapitools.json config to map a schema like RangeFilterInt to a custom DTO (MyApp.Models.Filters.CusomtDTO), the OpenAPI Generator still generates a new model class for RangeFilterInt instead of using the provided custom type.

test.yaml

openapi: 3.0.3
info:
  title: RangeFilter Demo
  version: 1.0.0
paths:
  /demo:
    get:
      summary: Test endpoint
      operationId: GetDemo
      parameters:
        - in: query
          name: range
          required: false
          schema:
            $ref: "#/components/schemas/RangeFilter"
      responses:
        '200':
          description: OK
components:
  schemas:
    RangeFilter:
      type: object
      properties:
        exactValue:
          type: integer
        minValue:
          type: integer
        maxValue:
          type: integer

openapitools.json

{
  "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "7.12.0"
  },
  "generatorName": "aspnetcore",
  "inputSpec": "test.yaml",
  "outputDir": "out",
  "templateDir": "custom-templates/",
  "additionalProperties": {
    "operationResultTask": true,
    "operationIsAsync": true,
    "buildTarget": "library",
    "nullableReferenceTypes": true,
    "useSwashbuckle": true,
    "useNewtonsoft": false,
    "sourceFolder": "src/",
    "packageName": "MyApp.Api",
    "modelPackage": "MyApp.Models.Generated",
    "sharedPackageName": "MyApp.Shared"
  },
  "importMappings": {
    "RangeFilter": "MyApp.Models.Filters.CustomDTO"
  },
  "typeMappings": {
    "RangeFilter": "MyApp.Models.Filters.CustomDTO"
  }
}

openapi-generator-cli generate -c openapitools.json 

OR

➜  filtersTest git:(main) ✗ openapi-generator-cli generate \
    -i test.yaml \
    -g aspnetcore \
    -o out \
    --import-mappings=RangeFilter=MyApp.Models.Filters.CustomDTO \
    --type-mappings=RangeFilter=MyApp.Models.Filters.CustomDTO

out/src/MyApp.Api/Controllers/DefaultApi.cs


/*
 * RangeFilter Demo
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.0
 * 
 * Generated by: https://openapi-generator.tech
 */
/*
 * RangeFilter Demo
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.0
 * 
 * Generated by: https://openapi-generator.tech
 */

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.SwaggerGen;
using MyApp.Api.Attributes;
using MyApp.Models.Generated;

namespace MyApp.Api.Controllers
{ 
    /// <summary>
    /// 
    /// </summary>
    [ApiController]
    public abstract class DefaultApiController : ControllerBase
    { 
        /// <summary>
        /// Test endpoint
        /// </summary>
        /// <param name="range"></param>
        /// <response code="200">OK</response>
        [HttpGet]
        [Route("/demo")]
        [ValidateModelState]
        [SwaggerOperation("GetDemo")]
        public abstract Task<IActionResult> GetDemo([FromQuery (Name = "range")]RangeFilter? range);
    }
}


✅ Expected Behavior
The generator should:
Use the mapped type MyApp.Models.Filters.CusomtDTO in the generated controller signature.

❌ Actual Behavior
Despite the following configuration:

"typeMappings": {
  "RangeFilterInt": "MyApp.Models.Filters.CusomtDTO"
},
"importMappings": {
  "RangeFilterInt": "MyApp.Models.Filters.CusomtDTO"
}

The generator:
Uses this generated type in controller signatures instead of the intended mapped custom DTO.

Image
openapi-generator version

"version": "7.12.0"

Generation Details
openapi-generator-cli generate -c openapitools.json 

OR

➜  filtersTest git:(main) ✗ openapi-generator-cli generate \
    -i test.yaml \
    -g aspnetcore \
    -o out \
    --import-mappings=RangeFilter=MyApp.Models.Filters.CustomDTO \
    --type-mappings=RangeFilter=MyApp.Models.Filters.CustomDTO
Steps to reproduce

in description

If is use kotlin for example I see the expected mapping for the field

➜  filtersTest git:(main) ✗ openapi-generator-cli generate \
    -i test.yaml \
    -g kotlin \
    -o out \
    --import-mappings=RangeFilter=MyApp.Models.Filters.CustomDTO \
    --type-mappings=RangeFilter=MyApp.Models.Filters.CustomDTO

........

     * @throws ServerException If the API returns a server error response
     */
    @Throws(IllegalStateException::class, IOException::class, UnsupportedOperationException::class, ClientException::class, ServerException::class)
    fun getDemo(range: MyApp.Models.Filters.CustomDTO? = null) : Unit {
        val localVarResponse = getDemoWithHttpInfo(range = range)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions