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

Absent type doesn't imply type object

Open wsalembi opened this issue 1 year ago • 1 comments

https://github.com/swagger-api/swagger-parser/commit/9a5cd1990b06374c75999288824dca83a2cef4e3 introduced the behavior that properties without type defined are implied of type object. I don't think this statement is true. The absence of type means it is any type (object, null, string, etc).

Example

paths:
  /foo:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Foo'
        required: true
      responses:
        200:
          description: ok
components:
  schemas:
    Foo:
      type: object
      allOf:
        - $ref: "#/components/schemas/Goo"
    Goo:
      type: object
      properties:
        goo:
          title: "Goo"

When resolving the schema, the type of property goo cannot be set to type object.

I removed the implication and it didn't break any other tests.

diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java	(revision 0ab2e8a7774b147e7b8c2c75b929089677d1b246)
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java	(date 1722333547892)
@@ -469,13 +469,13 @@
                 Schema property = updated.get(key);
 
                 if (property.getProperties() != model.getProperties()) {
-                    if (!hasSchemaType(property)) {
+                    /*if (!hasSchemaType(property)) {
                         if (SpecVersion.V30.equals(property.getSpecVersion())) {
                             property.setType("object");
                         } else {
                             property.addType("object");
                         }
-                    }
+                    }*/
                     model.addProperties(key, property);
                 } else {
                     LOGGER.debug("not adding recursive properties, using generic object");

wsalembi avatar Jul 30 '24 10:07 wsalembi

You have one incorrect assumption here.

The type keyword does not support null in OAS 3.0.x

You must use nullable as an alternative

null is not supported as a type (see nullable for an alternative solution)

Otherwise, I agree you cannot force type: object where any other primitive is a valid schema.

jeremyfiel avatar Sep 27 '24 12:09 jeremyfiel

@jeremyfiel Ok, I understand your remark about OAS 3.0.x but this is unrelated to the fix I propose. I want to remove the behavior of setting type object by default. The type must remain undefined.

@frantuma How can be proceed with this MR?

wsalembi avatar Nov 26 '24 09:11 wsalembi

Thanks for your report, fix will be provided in upcoming release by this PR #2202

daniel-kmiecik avatar Jun 17 '25 10:06 daniel-kmiecik