OpenAPITools / OpenAPITools/openapi-generator

[BUG][APEX] generated models duplicate property map for each transformed property

Open
#22,343 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

Schemas, requests, or responses, with two or more properties defined which are transformed this_id -> thisId cause APEX models generated to be invalid. For each transformed property the resulting class will have n x private static final Map<String, String> propertyMappings and public Map<String, String> getPropertyMappings() defined

openapi-generator version

7.17.0

OpenAPI declaration file content or url
{
  "openapi": "3.0.3",
  "info": {
    "title": "test API",
    "version": "65.0"
  },
  "paths": {

  },
  "components": {
    "schemas": {
      "RecordDetails": {
        "type": "object",
        "title": "RecordDetails",
        "properties": {
          "record_id": {
            "type": "integer",
            "title": "Record Id",
            "description": "Record identifier"
          },
          "name": {
            "type": "string",
            "title": "Name",
            "description": "Record name"
          },
          "other_id": {
            "type": "integer",
            "title": "Other Id",
            "description": "Other identifier"
          }
        },
        "required": [],
        "additionalProperties": false
      }
    }
  }
}
Results
/*
 * test API
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 65.0
 * 
 *
 * NOTE: This class is auto generated by the OAS code generator program.
 * https://github.com/OpenAPITools/openapi-generator
 * Do not edit the class manually.
 */

/**
 * OASRecordDetails
 */
public class OASRecordDetails {
    /**
     * Record identifier
     * @return recordId
     */
    public Integer recordId { get; set; }

    /**
     * Record name
     * @return name
     */
    public String name { get; set; }

    /**
     * Other identifier
     * @return otherId
     */
    public Integer otherId { get; set; }

    private static final Map<String, String> propertyMappings = new Map<String, String>{
        'record_id' => 'recordId',
        'other_id' => 'otherId'
    };

    public Map<String, String> getPropertyMappings() {
        return propertyMappings;
    }

    private static final Map<String, String> propertyMappings = new Map<String, String>{
        'record_id' => 'recordId',
        'other_id' => 'otherId'
    };

    public Map<String, String> getPropertyMappings() {
        return propertyMappings;
    }

    public static OASRecordDetails getExample() {
        OASRecordDetails recordDetails = new OASRecordDetails();
          recordDetails.recordId = 0;
          recordDetails.name = '';
          recordDetails.otherId = 0;
        return recordDetails;
    }

    public Boolean equals(Object obj) {
        if (obj instanceof OASRecordDetails) {           
            OASRecordDetails recordDetails = (OASRecordDetails) obj;
            return this.recordId == recordDetails.recordId
                && this.name == recordDetails.name
                && this.otherId == recordDetails.otherId;
        }
        return false;
    }

    public Integer hashCode() {
        Integer hashCode = 43;
        hashCode = (17 * hashCode) + (recordId == null ? 0 : System.hashCode(recordId));
        hashCode = (17 * hashCode) + (name == null ? 0 : System.hashCode(name));
        hashCode = (17 * hashCode) + (otherId == null ? 0 : System.hashCode(otherId));
        return hashCode;
    }
}
Expected
/*
 * test API
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 65.0
 * 
 *
 * NOTE: This class is auto generated by the OAS code generator program.
 * https://github.com/OpenAPITools/openapi-generator
 * Do not edit the class manually.
 */

/**
 * OASRecordDetails
 */
public class OASRecordDetails {
    /**
     * Record identifier
     * @return recordId
     */
    public Integer recordId { get; set; }

    /**
     * Record name
     * @return name
     */
    public String name { get; set; }

    /**
     * Other identifier
     * @return otherId
     */
    public Integer otherId { get; set; }

    private static final Map<String, String> propertyMappings = new Map<String, String>{
        'record_id' => 'recordId',
        'other_id' => 'otherId'
    };

    public Map<String, String> getPropertyMappings() {
        return propertyMappings;
    }

    public static OASRecordDetails getExample() {
        OASRecordDetails recordDetails = new OASRecordDetails();
          recordDetails.recordId = 0;
          recordDetails.name = '';
          recordDetails.otherId = 0;
        return recordDetails;
    }

    public Boolean equals(Object obj) {
        if (obj instanceof OASRecordDetails) {           
            OASRecordDetails recordDetails = (OASRecordDetails) obj;
            return this.recordId == recordDetails.recordId
                && this.name == recordDetails.name
                && this.otherId == recordDetails.otherId;
        }
        return false;
    }

    public Integer hashCode() {
        Integer hashCode = 43;
        hashCode = (17 * hashCode) + (recordId == null ? 0 : System.hashCode(recordId));
        hashCode = (17 * hashCode) + (name == null ? 0 : System.hashCode(name));
        hashCode = (17 * hashCode) + (otherId == null ? 0 : System.hashCode(otherId));
        return hashCode;
    }
}
Generation Details
yarn openapi-generator-cli generate -g apex -o output/test -i 
Steps to reproduce
  1. create openapi.json with the provided spec
  2. Run yarn openapi-generator-cli generate -g apex -o output/test -i {yourpathtofile}
Related issues/PRs
Suggest a fix

I believe the logic in the ApexCodegen is causing duplicate maps,

https://github.com/OpenAPITools/openapi-generator/blob/1e3f6d0ed94c64c08557fc2778f980d407d83453/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractApexCodegen.java#L440

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractApexCodegen.java around line 440, then reproduce with the supplied OpenAPI declaration and the yarn openapi-generator-cli command. Done means the generated Apex model contains one propertyMappings map and one getPropertyMappings method, even when multiple properties are transformed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.