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

Examples of excluding certain types in model schemas

Open huangsam opened this issue 13 years ago • 8 comments

I chatted with @fehguy on IRC to find a valid approach for filtering unnecessary key-values and JSON nodes away from the model schema. Here's a picture of what I'm aiming to do:

remove-these

There are two approaches to this problem. One option is to override the model completely. The other option is to implement a ModelConverter, where one can choose what field types to exclude.

Can you guys please provide examples of excluding certain types? Thanks!

PS: I'm using the java-play2 framework

huangsam avatar Nov 05 '13 02:11 huangsam

Just pushed a unit test (and a fix) to do this:

https://github.com/wordnik/swagger-core/blob/develop/modules/swagger-core/src/test/scala/converter/CustomConverterTest.scala#L47

This is being deployed to 1.3.1-SNAPSHOT in sonatype right now.

fehguy avatar Nov 07 '13 18:11 fehguy

looks like that caused a test regression. Will update shortly

fehguy avatar Nov 07 '13 18:11 fehguy

Thanks for attending this issue. Waiting for your update.

huangsam avatar Nov 07 '13 20:11 huangsam

added example and uploaded an update to swagger-core-1.3.1-SNAPSHOT to support a new field type:

https://github.com/wordnik/swagger-core/wiki/Overriding-Models

fehguy avatar Nov 08 '13 14:11 fehguy

Hi fehguy, the converter approach looks good.can you plz let us know where to add that piece of code in playframework ( in swagger classes o anywhere else) .we are using swagger 1.2.5

infyravi avatar Nov 12 '13 09:11 infyravi

Hi, you can get it in swagger-play2-1.3.1, which is now on maven central.

fehguy avatar Nov 12 '13 17:11 fehguy

Hi,

By adding the below code in swagger.js, we can avoid unnecessary key-values and JSON nodes.

//Add below code in getMockSignature function to avoid unnecessary occurrence of model properties.

  var str=returnVal.search("<span class=\"strong\">EntityBeanIntercept {");
  if(str==0){
    return "";
  }
  else{
     returnVal=returnVal.replace("<div><span class=\"propName propOpt\">_ebean_intercept</span> (<span class=\"propType\">EntityBeanIntercept</span>, <span class=\"propOptKey\">optional</span>),</div>","");
    return returnVal;
   }

//Add below code in createJSONSample function to avoid unnecessary occurrence of JSON node

  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    prop = _ref[_i];
       if(prop.name!="_ebean_intercept"){
        result[prop.name] = prop.getSampleValue(modelsToIgnore);
    }
  }

Chitra1986 avatar Nov 14 '13 13:11 Chitra1986

this definitely will clean things up in the UI. But if you use the client generators, etc., you'll have the same problem. I think the best thing to do is the model converter technique.

fehguy avatar Nov 14 '13 15:11 fehguy