swagger-api / swagger-api/swagger-codegen

Inconsistent instantiation per language of nested objects

Open
#2,281 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Consider the following specification:

{
    "basePath": "/some/v1/path",
    "consumes": [
        "application/json"
    ],
    "definitions": {
        "Parent": {
            "properties": {
                "child": {
                    "$ref": "#/definitions/Child"
                },
            },
            "type": "object"
        },
        "Child": {
            "properties": {
                "boolVal": {
                    "description": "Some boolean",
                    "type": "boolean"
                }
            },
            "type": "object"
        },
        "ParentsResponse": {
            "properties": {
                "data": {
                    "items": {
                        "$ref": "#/definitions/Parent"
                    },
                    "type": "array"
                },
            },
            "type": "object"
        },
    },
    "host": "www.somewhere.com",
    "info": {
        "description": "Nested class example",
        "title": "Nested Class Example",
        "version": "1"
    },
    "paths": {
        "/parents": {
            "post": {
                "description": "Creates a parent object.",
                "parameters": [
                    {
                        "description": "The parent object to be created.",
                        "in": "body",
                        "name": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/Parent"
                        }
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "default": {
                        "description": "Default response",
                        "schema": {
                            "$ref": "#/definitions/ParentsResponse"
                        }
                    }
                },
                "security": [
                    {
                        "basicAuth": []
                    }
                ],
                "summary": "Creates a parent object."
            }
        },
    },
    "produces": [
        "application/json"
    ],
    "schemes": [
        "http"
    ],
    "securityDefinitions": {
        "basicAuth": {
            "description": "basic authentiation",
            "type": "basic"
        }
    },
    "swagger": "2.0"
}

There is a Parent which contains a Child. When I use various code generators, and instantiate an object of type Parent, it may or may not have its contained Child object created. For instance,in Java and Perl the child is NULL/undefined, whereas in Javascript and C++ the child is created.

This inconsistency has surprised some users of my API. I would think that the behaviour should be consistent. Furthermore, I would think that the contained objects should be created in all cases. In my actual schema the degree of nesting is much greater than 1, and when the generated client library does not do the allocations for the user it is quite a pain.

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 supplied Swagger 2.0 schema and compare how generated Parent and Child objects are instantiated in Java, Perl, JavaScript, and C++. Done means the generated clients have a documented, consistent policy for nested-object initialization across the affected languages.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, java, javascript, openapi, perl
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.