OpenAPITools / OpenAPITools/openapi-generator

[BUG] Body argument is treated as plain map rather than object when `additionalProperties` is `true`

Open
#11,049 1 comment 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

When generating an API client for and endpoint whose requestBody's schema allows additional properties the arguments of an API call function drop to a plain map rather than a typed structure, e.g.:

typescript-axios

//...
    public relaxedPost(requestBody: { [key: string]: object; }, options?: AxiosRequestConfig) {
        return DefaultApiFp(this.configuration).relaxedPost(requestBody, options).then((request) => request(this.axios, this.basePath));
//...
    public strictPost(strict: Strict, options?: AxiosRequestConfig) {
        return DefaultApiFp(this.configuration).strictPost(strict, options).then((request) => request(this.axios, this.basePath));
    }
}

java:

//...
    public okhttp3.Call relaxedPostCall(Map<String, Object> requestBody, final ApiCallback _callback) throws ApiException {
//...
    public okhttp3.Call strictPostCall(Strict strict, final ApiCallback _callback) throws ApiException {

I want in both cases the body argument to be typed according to relevant type definition Relaxed and Strict like this:

typescript-axios

//...
    public relaxedPost(relaxed: Relaxed, options?: AxiosRequestConfig) {
        return DefaultApiFp(this.configuration).relaxedPost(requestBody, options).then((request) => request(this.axios, this.basePath));
//...
    public strictPost(strict: Strict, options?: AxiosRequestConfig) {
        return DefaultApiFp(this.configuration).strictPost(strict, options).then((request) => request(this.axios, this.basePath));
    }
}

java:

//...
    public okhttp3.Call relaxedPostCall(Relaxed relaxed, final ApiCallback _callback) throws ApiException {
//...
    public okhttp3.Call strictPostCall(Strict strict, final ApiCallback _callback) throws ApiException {
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
openapi-generator version
openapi-generator-cli 5.3.1-SNAPSHOT
  commit : eeb00e2
  built  : 2021-11-20T01:56:03Z
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url
openapi: '3.0.2'
info:
  title: API Title
  version: '1.0'
servers:
  - url: https://api.server.test/v1
paths:
  /strict:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Strict'
      responses:
        '200':
          description: OK

  /relaxed:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Relaxed'
      responses:
        '200':
          description: OK



components:
  schemas:
    Strict:
      title: Strict
      additionalProperties: false
      properties:
        id:
          type: string
        name:
          type: string
      required:
        - id
        - name
    Relaxed:
      title: Relaxed
      additionalProperties: true
      properties:
        id:
          type: string
        name:
          type: string
      required:
        - id
        - name
Generation Details

Create a script: generate.sh

#!/usr/bin/env bash

INPUT=$1
LANGUAGE=$2

OUTPUT_DIR="open-api-generator/$INPUT/$LANGUAGE"

mkdir -p $OUTPUT_DIR

set -x

docker run --rm \
    -v $PWD:/local openapitools/openapi-generator-cli generate \
    -i "local/$INPUT" \
    -g $LANGUAGE \
    -o "/local/$OUTPUT_DIR"
Steps to reproduce

Run it as:

./generate.sh bug.yaml typescript-axios
./generate.sh bug.yaml java
Related issues/PRs
Suggest a fix

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 with the OpenAPI declaration and generate.sh reproduction using the typescript-axios and java generators. Compare the generated relaxedPost and strictPost signatures with the expected Relaxed and Strict types. Done means request bodies with additionalProperties: true retain their named schema types in both generated clients.

Written by the indexing model from the issue text.

Assessment

Tech stack
bash, docker, java, openapi, typescript
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.