Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] "additionalProperties: true" for free form object in directory/map is not working #796

Closed
yuanpli opened this issue Aug 13, 2018 · 7 comments

Comments

@yuanpli
Copy link

yuanpli commented Aug 13, 2018

Description

I am using openapi-generator-maven-plugin to generate the model pojo beans, but the hashmap object is not generated correct if "additionalProperties: true" introduced in declaration file.

openapi-generator version
   <version>3.2.0</version>
OpenAPI declaration file content or url
swagger: '2.0'
info:
  description: >-
    This spec is mainly for testing Petstore server and contains fake endpoints,
    models. Please do not use this for any other purpose. Special characters: "
    \
  version: 1.0.0
  title: Swagger Petstore
  termsOfService: 'http://swagger.io/terms/'
  contact:
    email: apiteam@swagger.io
  license:
    name: Apache-2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
host: 'petstore.swagger.io:80'
basePath: /v2
paths:
  /test:
    get:
      description: Gets all operation occurrence resources.
      responses:
        '200':
          description: Successful response
          schema:
            type: string
        default:
          description: Error payload
          schema:
            type: string
      tags:
        - test
schemes:
  - http
definitions:
  MapTest1:
    description: This type is represented in JSON as an extensible JSON object
    type: object
    additionalProperties: {}
  MapTest2:
    description: This type is represented in JSON as an extensible JSON object
    type: object
    additionalProperties: true
    example:
      optional_key: optional value
  MapTest3:
    type: object
    additionalProperties:
      type: object
externalDocs:
  description: Find out more about Swagger
  url: 'http://swagger.io'
Command line used for generation

mvn clean compile

Steps to reproduce

The maven pom.xml is

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.vnfm.model</groupId>
    <artifactId>swagger-vnfm</artifactId>
    <packaging>jar</packaging>
    <version>DYNAMIC-SNAPSHOT</version>
    <name>swagger-vnfm</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger-core-version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson-version}</version>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <swagger-core-version>1.5.21</swagger-core-version>
        <gson-version>2.8.1</gson-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <!--<groupId>io.swagger</groupId>-->
                <!--<artifactId>swagger-codegen-maven-plugin</artifactId>-->
                <!--<version>2.3.1</version>-->
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>lcm-generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/swagger.yaml</inputSpec>
                            <language>java</language>
                            <generateApis>false</generateApis>
                            <generateModelTests>false</generateModelTests>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <generateSupportingFiles>false</generateSupportingFiles>
                            <skipOverwrite>true</skipOverwrite>
                            <configOptions>
                                <dateLibrary>java8</dateLibrary>
                            </configOptions>
                            <output>${project.build.directory}/generated-sources</output>
                            <modelPackage>com.vnfm.datamodel.cbam.v4</modelPackage>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Suggest a fix/enhancement

Free-form objects can be defined in two ways as described in https://swagger.io/docs/specification/data-models/dictionaries/ . And "additionalProperties: {}" is working, so "additionalProperties: true" should be also supported.

@jmini jmini changed the title [openapi-generator-maven-plugin] "additionalProperties: true" for free form object in directory/map is not working [java] "additionalProperties: true" for free form object in directory/map is not working Aug 13, 2018
@jmini
Copy link
Member

jmini commented Aug 13, 2018

Related issue: #409 (this was only handling the case additionalProperties: false)

Can you post some examples of the expected changes to the generated code? I guess you expect something else for MapTest1, MapTest2 or MapTest3. More description would help.

@yuanpli
Copy link
Author

yuanpli commented Aug 14, 2018

Hi,

Based on my testing, MapTest1 generates the code like

public class MapTest1 extends HashMap<String, Object> {
......
}

And MapTest3 also generates the code like

public class MapTest3 extends HashMap<String, Object> {
......
}

When should the value of properties additionalProperties be true and when can be false? There is no clear description in the spec OAS3 definition for Schema Object.

But there is a declaration from Free-Form Objects that additionalProperties: true is equal to additionalProperties: {}. Right?

So what I expect for MapTest2 should also generate the code like below

public class MapTest2 extends HashMap<String, Object> {
......
}

@yuanpli
Copy link
Author

yuanpli commented Aug 17, 2018

Hi, there is any update or share your opinion. Thanks!

@wing328
Copy link
Member

wing328 commented Aug 17, 2018

I believe you want to describe arbitrary JSON, please use "type": "object" instead. Here is an example in JSON:

    "responses": {
      "200": {
        "description": "successful operation",
        "schema": {
          "type": "object"
        }
      }
    }

Ref: https://stackoverflow.com/questions/35782175/how-to-receive-a-dynamic-response-in-a-swagger-spec

@jmini
Copy link
Member

jmini commented Aug 17, 2018

@wing328 there is probably not that much difference between a Free-Form Object:

    "responses": {
      "200": {
        "description": "successful operation",
        "schema": {
          "type": "object",
          "additionalProperties": "true"
        }
      }
    }

and an object:

    "responses": {
      "200": {
        "description": "successful operation",
        "schema": {
          "type": "object"
        }
      }
    }

But we should support the free form object.


In a java (for example), having an Object or a Map<String, Object> is not the same, even if the JSON payload is the same.

Maybe my fix in a02b313 was not that great and need to be ajusted.

jmini added a commit to jmini/openapi-generator that referenced this issue Aug 20, 2018
Consider "additionalProperties: true"

Fixes OpenAPITools#796
@jmini
Copy link
Member

jmini commented Aug 20, 2018

I have filed a PR: #849

@jmini
Copy link
Member

jmini commented Aug 21, 2018

Can you give version 3.2.2-SNAPSHOT a try?

A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this issue Feb 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants