swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

MapProperty doesn't return both properties and additionalProperties

Open Kporal opened this issue 8 years ago • 4 comments

Affects: 1.5.12

Issue

When deserializing an inline schema with MapProperty, I can't find the properties in the POJO, only the additional properties.

The specification does not say that additional properties and properties are mutually exclusive so I'd expect to find the properties in the POJO.

In addition, when I parse a definition with ModelImpl I can access to both properties and additionalProperties.

The Swagger editor doesn't show any error with my Model (see below).

How to reproduce

My Swagger file looks like this:

---
swagger: "2.0"
info:
 version: 1.0.0
 title: Responses with additionalProperties

paths:
  /resource:
    get:
      responses:
        default:
          description: "OK"
          schema:
            '$ref': '#/definitions/TheOKModel'
    post:
      responses:
        default:
          description: "OK"
          schema:
            type: object
            properties:
              name:
                type: string
            additionalProperties:
              type: string
definitions:
  TheOKModel:
    type: object
    properties:
      name:
        type: string
    additionalProperties:
      type: string

And I'd expect this test to pass:

import io.swagger.models.ModelImpl;
import io.swagger.models.Swagger;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.util.Yaml;
import org.junit.Assert;
import org.junit.Test;

public class SwaggerBug {

    @Test
    public void testOK() throws Exception {
        final Swagger swagger = Yaml.mapper().readValue(getClass().getResourceAsStream("swaggerTest.yml"),
                Swagger.class);
        final ModelImpl theOKModel = (ModelImpl) swagger.getDefinitions().get("TheOKModel");
        Assert.assertEquals(StringProperty.TYPE, theOKModel.getAdditionalProperties().getType());
        Assert.assertEquals(1, theOKModel.getProperties().size());
    }

    @Test
    public void testKO() throws Exception {
        final Swagger swagger = Yaml.mapper().readValue(getClass().getResourceAsStream("swaggerTest.yml"),
                Swagger.class);
        final MapProperty theProblematicModel = (MapProperty) swagger
                .getPath("/resource")
                .getPost()
                .getResponses()
                .get("default")
                .getSchema();
        Assert.assertEquals(StringProperty.TYPE, theProblematicModel.getAdditionalProperties().getType());
        //TODO: make that compile and pass
        // Assert.assertEquals(1, theProblematicModel.getProperties().size());
    }
}

Kporal avatar Sep 25 '17 16:09 Kporal

Hi @Kporal,

When additionalProperties is provided in the response schema, MapProperty is mapped where the field 'properties' (Map<String, Property>) is missing which cause this issue.

I am not sure is it intentional, but it looks like issue as field 'properties' info is missing and it should to be added.

Thanks, Mohammed

ymohdriz avatar Nov 14 '17 03:11 ymohdriz

Yup, it was never implemented that way. PRs are welcome.

webron avatar Nov 14 '17 19:11 webron

Hi @webron,

Shall I add the properties field in MapProperty and go ahead with the pull request?

Thanks, Mohammed

ymohdriz avatar Nov 15 '17 19:11 ymohdriz

hello, is there any news about this topic?

thboileau avatar May 13 '24 08:05 thboileau