Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonView support #2079

Closed
monoc44 opened this issue Jan 11, 2017 · 2 comments
Closed

JsonView support #2079

monoc44 opened this issue Jan 11, 2017 · 2 comments

Comments

@monoc44
Copy link

monoc44 commented Jan 11, 2017

Dear team,

JSON views are a very convenient and powerful way to not duplicate models used by resources. Many developers already rose this point and believe Swagger should support it. However it seems that all the requests to support this feature have been turned down.

What about generating a single and individual definition entry when @JSONVIEW is detected?

For instance, taking this example below (pseudo code):

@GET(/serviceA)
@JsonView(ViewA.class)
public MyModel methodA() {...}

@GET(/serviceB)
@JsonView(ViewB.class)
public MyModel methodB() {...}
class MyModel {

  Integer id;

  @JsonView(ViewA.class)
  String propertyForServiceA;

  @JsonView(ViewA.class)
  String propertyNotForServiceB;

  @JsonView(ViewB.class)
  String propertyForServiceB;
  ...
}

=> parsing these two resources will translate today into the following OAI JSON file:

paths: {
   /serviceA:
     get ->  responses: { 200-> $ref: #/definitions/MyModel }
   /serviceB: {
     get ->  responses: { 200-> $ref: #/definitions/MyModel }
},
definitions: {
  MyModel:
      type: object,
      properties: [
        id,
        propertyForServiceA,
        propertyNotForServiceB,
        propertyForServiceB
      ]
}

Sadly, only one definition model will be generated after Swagger scans either serviceA or serviceB.

Would it be difficult to instead generate a 'contextualized' definition model associated to each individual service, hence 'supporting' JsonViews?

The generated OAI file could then look something like:

paths: {
   /serviceA:
     get ->  responses: { 200-> $ref: #/definitions/MyModel.serviceA.methodA}
   /serviceB: {
     get ->  responses: { 200-> $ref: #/definitions/MyModel.serviceB.methodB}
},
definitions: {
  MyModel.serviceA.methodA:
      className: xxx.xxx.MyModel
      type: object,
      properties: [
        id,
        propertyForServiceA,
        propertyNotForServiceB
      ]
  MyModel.serviceB.methodB:
      className: xxx.xxx.MyModel      
      type: object,
      properties: [
        id,
        propertyForServiceB
      ]
}

Please let me know what you think. Thanks.

@fehguy
Copy link
Contributor

fehguy commented Jan 11, 2017

Hi, a bit of confusion. There's no philosophical reason why they're not supported--they're "just not supported" so sending a PR would be just fine. But we do need to adhere to the specification so things like className are illegal as you're proposing.

I would need to think more about what you're actually proposing but please don't get me wrong--there's nothing wrong with @JsonView other than the fact that it's more work, and any implementation of it inside swagger-core needs to adhere to the spec.

p4ali pushed a commit to p4ali/swagger-core that referenced this issue Feb 14, 2018
See original request swagger-api#2079

The patch will create various definition models for response based on the original model, plus
the JsonView, e.g., given the original model Car, and JsonView named Detail and Summary, the
definition model could be Car_Summary, Car_Detail, or Car_Summary-or-Detail.

The patch also support the inheritance of the JsonView, e.g., The Detail is a subtype of Summary,
so any type/field annotated by Summary, should be visible to Detail view.

Please refer to the JsonViewTest.java for detail.
Also refer to the CarResource.java and the updated ApiListingResourceIT.java
frantuma pushed a commit that referenced this issue Feb 22, 2018
See original request #2079

The patch will create various definition models for response based on the original model, plus
the JsonView, e.g., given the original model Car, and JsonView named Detail and Summary, the
definition model could be Car_Summary, Car_Detail, or Car_Summary-or-Detail.

The patch also support the inheritance of the JsonView, e.g., The Detail is a subtype of Summary,
so any type/field annotated by Summary, should be visible to Detail view.

Please refer to the JsonViewTest.java for detail.
Also refer to the CarResource.java and the updated ApiListingResourceIT.java
frantuma added a commit that referenced this issue Feb 23, 2018
 refs #2079 - JsonView support (includes and replaces #2662)
@frantuma
Copy link
Member

implemented in #2662 and #2681

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants