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] etc.: generated code is incorrect for untyped schemas #6896

Open
gibson042 opened this issue Nov 7, 2017 · 1 comment
Open

[JAVA] etc.: generated code is incorrect for untyped schemas #6896

gibson042 opened this issue Nov 7, 2017 · 1 comment

Comments

@gibson042
Copy link

Description

Generated code for schemas with no defined type is incorrect, because parsing seems to require a type property despite it being optional in the specification (where it defines values that can be of any type).

Swagger-codegen version

2.2.3, 2.3.0-SNAPSHOT

Swagger declaration file content or url
swagger: '2.0'
info:
  title: 'map of arbitrary values'
  version: '1'
produces: ["application/json"]
paths:
  /mapOfAnything:
    get:
      operationId: mapOfAnything
      responses:
        default:
          schema:
            description: 'map of arbitrary values'
            type: object
            additionalProperties:
              description: Any JSON type
Command line used for generation

./run-in-docker.sh generate -i swagger.yaml -l java

Steps to reproduce

Generate code from the above YAML. Observe that the response is treated as Object instead of Map<String, Object>, despite describing a map.

Related issues/PRs

None found.

Suggest a fix/enhancement

Treat untyped schemas as generically as possible.

gibson042 added a commit to gibson042/swagger-parser that referenced this issue Nov 7, 2017
gibson042 added a commit to gibson042/swagger-codegen that referenced this issue Nov 7, 2017
gibson042 added a commit to gibson042/swagger-parser that referenced this issue Feb 14, 2018
gibson042 added a commit to gibson042/swagger-codegen that referenced this issue May 8, 2018
gibson042 added a commit to gibson042/swagger-codegen that referenced this issue Sep 20, 2018
@n0v1
Copy link

n0v1 commented Oct 22, 2018

We have the same problem with Java CodeGen and additionalProperties without a specific type. We have a meta field in our response structure that may contain arbitrary properties. So, ideally we'd like to use something like that

swagger: "2.0"

info:
  version: "1.0.0"
  title: "Test"

paths:
  /test:
    get:
      description: "Test request"
      responses:
        200:
          description: "Successful operation"
          schema:
            $ref: "#/definitions/Meta"

definitions:
  Meta:
    description: Key value map for meta data.
    type: object
    additionalProperties: {}

to make it explicit that there are no restrictions on the type of additional properties. With that the Meta class is of type Object (public class Meta), but should be HashMap<String, Object> (public class Meta extends HashMap<String, Object>)

As a hack to work around this issue, we use

additionalProperties:
  type: object

This fixes the problem with Java CodeGen but leads to validation errors when validating our response data since most meta properties are simple types like strings and integers instead ob objects.

I would be great if the pull requests from @gibson042 could get merged to fix this problem.

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

2 participants