How do I use swagger api to generate swagger with additionalProperties as false for object

1,275 views
Skip to first unread message

Yuting

unread,
Sep 28, 2017, 3:15:55 AM9/28/17
to Swagger
I somehow have to generate a swagger doc with addtionalProperties as false to not allow additional properties.  I looked at https://github.com/swagger-api/swagger-core/blob/master/modules/swagger-models/src/main/java/io/swagger/models/ModelImpl.java

but setAdditionalProperties accept a Property only, how do I set it with a boolean value?

public void setAdditionalProperties(Property additionalProperties) {
type(OBJECT);
this.additionalProperties = additionalProperties;
}


"TestDTO" : {
      "type" : "object",
      "additionalProperties":false
      "properties" : {
        "property1" : {
          "description" : "This is a test object.",
          "$ref" : "#/definitions/TestDTO"
        },
        "perperty2" : {
          "type" : "string",
          "description" : "this is a property."
        }
      }
    },

Ron Ratovsky

unread,
Sep 28, 2017, 4:22:35 PM9/28/17
to swagger-sw...@googlegroups.com

The 2.0 specification doesn’t allow setting additionalProperties: false. It was intended to be the default definition but was never clarified in the spec.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yuting

unread,
Sep 28, 2017, 9:33:48 PM9/28/17
to Swagger
Thanks Ron for the response!

Is it possible in swagger api 2.0.0-rc1?  I explored a bit but didn't find many clue.
The 3.0 spec does explicitly say value can be boolean though.
  • additionalProperties - Value can be boolean or object. Inline or referenced schema MUST be of a Schema Objectand not a standard JSON Schema.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Ben Sayers

unread,
Sep 28, 2017, 9:46:24 PM9/28/17
to Swagger
I believe the Swagger 2 specification does indeed allow additional properties and that the default value is an empty schema. Here is my reasoning:

This is the Swagger 2 definition for additional properties.

The following properties are taken from the JSON Schema definition but their definitions were adjusted to the Swagger Specification. Their definition is the same as the one from JSON Schema, only where the original definition references the JSON Schema definition, the Schema Object definition is used instead.
items
allOf
properties
additionalProperties

And this is the JSON Schema draft 4 specification's definition of additional properties

5.4.4.  additionalProperties, properties and patternProperties
5.4.4.1.  Valid values
   The value of "additionalProperties" MUST be a boolean or an object.
   If it is an object, it MUST also be a valid JSON Schema.

Swagger 2 uses the same definition from the JSON Schema specification unless the value is a JSON Schema. Since the value false is a Boolean we take the definition from the JSON Schema specification which says it is allowed.

In regards to the default value again we take the definition from the JSON Schema draft 4 specification:

5.4.4.3.  Default values
   If either "properties" or "patternProperties" are absent, they can be
   considered present with an empty object as a value.
   If "additionalProperties" is absent, it may be considered present
   with an empty schema as a value.

Sources:

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Ron Ratovsky

unread,
Sep 28, 2017, 10:16:54 PM9/28/17
to swagger-sw...@googlegroups.com

Yes, the 3.0.0 spec was changed to allow that. I’m not sure if we support it yet in swagger-core 2.0. Mind filing a ticket?

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--

You received this message because you are subscribed to the Google Groups "Swagger" group.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Ron Ratovsky

unread,
Sep 28, 2017, 10:22:00 PM9/28/17
to swagger-sw...@googlegroups.com

Ben,

 

I realize this is might sound a bit arrogant, but that’s not the intent.

Being the person who wrote the Swagger 2 specification, and spent hours specifically on the Schema Object itself, I can assure you that the intent was as I stated before. This is a mistake in the wording of the spec which was never clarified properly – and it’s also the reason why the wording in the 3.0.0 spec around additionalProperties is much more explicit.

 

 

 

From: <swagger-sw...@googlegroups.com> on behalf of Ben Sayers <bsa...@atlassian.com>


Reply-To: "swagger-sw...@googlegroups.com" <swagger-sw...@googlegroups.com>
Date: Thursday, September 28, 2017 at 18:46
To: Swagger <swagger-sw...@googlegroups.com>

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--

You received this message because you are subscribed to the Google Groups "Swagger" group.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Ben Sayers

unread,
Sep 28, 2017, 10:35:50 PM9/28/17
to Swagger
Fair enough.

But perhaps the ship has sailed? A number of Swagger tools I have used allow booleans in additional properties and validate them correctly. For example the official json schema for Swagger 2 allows booleans in additional properties:

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Ron Ratovsky

unread,
Sep 28, 2017, 10:55:11 PM9/28/17
to swagger-sw...@googlegroups.com

I know. I’m embarrassed to say that that’s my mistake too

Regardless, it’s not going to change in the Swagger-provided tools.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Yuting

unread,
Sep 29, 2017, 12:10:55 AM9/29/17
to Swagger

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages