binlecode / grails-swagger

Grails Swagger Plugin
0 stars 0 forks source link

Enhance the Object to Json Serializer to strip Grails Gorm traits #9

Open kblief opened 6 years ago

kblief commented 6 years ago

As a api user, I want to example json to ONLY contain the fields that will be serialized to JSON when invoking APIs.

Grails Domain Classes list additional trait attributes that are not serialized to JSON when invoking the API calls.

Some Examples

"dbo": {
        "additionalProp1": {},
        "additionalProp2": {},
        "additionalProp3": {}
      },
      "errors": {
        "nestedPath": "string",
        "allErrors": [
          {
            "codes": [
              "string"
            ],
            "arguments": [
              {}
            ],
            "defaultMessage": "string",
            "objectName": "string",
            "code": "string"
          }
        ],
        "globalErrorCount": 0,

Also does not work for Grails Command Objects

binlecode commented 6 years ago

looking into swagger-core lib where the model json converter is loaded. the possible solution is to override with custom converter to filter out unwanted bean properties: https://github.com/swagger-api/swagger-core/wiki/overriding-models

binlecode commented 6 years ago

Adding a custom model converter to remove spring Errors property from Swagger model to json rendering. Also add an example jsonIgnore (from Jackson) annotation to example Book domain to block some Grails domain traits as global json marshalling configuration. This should be used with caution with specific RESTful resource response content exposure considerations.

binlecode commented 6 years ago

One of the challenge is Swagger model overriding can not resolve a property by its name. This is due to the underlying Jackson objectMapper is only resolving object tree by type. Therefore, for some Grails domain traits such as 'version' or 'dirty', which are of primitive types (String, Boolean, etc), there's no way for Jackson objectMapper to tell them apart from other general bean properties.

binlecode commented 6 years ago

v1.2.1 merged to dev-g32x main branch: https://github.com/binlecode/swagger/tree/dev-grails-32x

binlecode commented 6 years ago

Not quite happy with current solution that a Grails domain class has to manually add "@JsonIgnoreProperties" annotation to mute some GORM traits fields. Need to find a systematic way of filtering out those fields at swagger document generation runtime.