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

Swagger 2.0 ModelConverter support for required/not required items #2420

Closed
pjfanning opened this issue Sep 9, 2017 · 11 comments
Closed

Swagger 2.0 ModelConverter support for required/not required items #2420

pjfanning opened this issue Sep 9, 2017 · 11 comments

Comments

@pjfanning
Copy link
Contributor

With the Required field having moved to the parent property (example), I'm wondering how to get swagger-scala-module ModelConverter to be able to control which items in a case class are marked as required. The resolve method doesn't appear to expose the parent property. Is there another way to do this, or do we need to change the ModelConverter resolve to allow access to the parent property?

@pjfanning
Copy link
Contributor Author

@webron @frantuma would either of you know the answer to this question? I have a partially working port of the swagger-scala-module to use io.swagger.v3.core but I haven't found a solution for this.

@frantuma
Copy link
Member

@pjfanning I am not sure I understand what you mean; can you provide some more details about your case?

@pjfanning
Copy link
Contributor Author

pjfanning commented Dec 18, 2017

https://github.com/swagger-api/swagger-scala-module/blob/develop/src/main/scala/io/swagger/scala/converter/SwaggerScalaModelConverter.scala#L65

The setRequired(boolean) method is no longer supported in swagger-core v2.0.0. As far as I can work out, the code will need to call setRequired(Collection) on the parent property. I'm not sure how this can be achieved in the ModelConverter and it does not seem to provide access to parent properties.

@frantuma
Copy link
Member

There is an ongoing effort to rationalize converter/resolver code tracked in #2593, with just merged PR #2594 implementing a first phase which can help with scala converter issue, as it adds parent resolved schema as parameter of ModelConverter Schema resolveAnnotatedType method (equivalent to resolveProperty method in 1.5).

Let me know if this can help, also added a note in #2593 to keep track of any further changes affecting / involving updates of scala converter project.

@pjfanning
Copy link
Contributor Author

Closed due to #2593

@pjfanning pjfanning reopened this Apr 1, 2018
@pjfanning
Copy link
Contributor Author

pjfanning commented Apr 1, 2018

@frantuma I had a look after the swagger-core 2.0.0 release but still can't work out a way to rewrite the swagger-scala-module ModelConverter to properly support optional properties. Would you be able to review my comment from Dec 18?
I tried to use the AnnotatedType getParent method and this does seem to return non-null values in some cases - but what I don't have is the name of field so I can't called type.getParent.addRequiredItem(fieldName). The AnnotatedType getName is always null in the test cases that appear in swagger-scala-module. Is there another way to get the name of the field for which the resolve method has been called for?
For example if you have case class MyClass(field: String), I need the ModelConverter to have access to the name of the property being resolved, eg field. In this case, field is a required value.

@pjfanning
Copy link
Contributor Author

@frantuma @webron would it be possible to some pointers on how I might solve this issue?
If I got this solved, I think I'll be in a position to release a swagger 2.0 based version of swagger-akka-http (which contains a copy of swagger-scala-module).

@frantuma
Copy link
Member

frantuma commented Aug 9, 2018

@pjfanning the name of the field is resolved (if not passed in annotatedType param) in resolveAnnotatedType method; not sure how is your code organized, (e.g. if you're extending ModelResolver or such), currently e.g. overriding protected String decorateModelName(AnnotatedType type, String originalName) to store the name somewhere could be an option, I would need some more details about your code.

@pjfanning
Copy link
Contributor Author

pjfanning commented Aug 9, 2018

@frantuma thanks for your suggestions
The class is https://github.com/swagger-api/swagger-scala-module/blob/develop/src/main/scala/io/swagger/scala/converter/SwaggerScalaModelConverter.scala

My partial rewrite for swagger 2.0.0 is https://github.com/pjfanning/swagger-scala-module/blob/swagger-core-2.0.0/src/main/scala/io/swagger/scala/converter/SwaggerScalaModelConverter.scala

I tried you idea about SwaggerScalaModelConverter extending ModelResolver and overriding decorateModelName but this turns out not to be what I need. decorateModelName gives the model name but I need the fieldName for addRequiredItem.

In my example above (case class MyClass(field: String)), the name that I need to get is field not the type name (String in this case).

Is there any chance that resolve(AnnotatedType annotatedType, ModelConverterContext context, Iterator<ModelConverter> next) could be modified to pass the propertyName too?

https://github.com/swagger-api/swagger-core/blob/master/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java#L1008

frantuma added a commit that referenced this issue Aug 10, 2018
ref #2420 - adds propertyName to annotatedType
@frantuma
Copy link
Member

@pjfanning not sure if this helps in all cases, however #2909 adds propName to annotatedType, try to give it a go.

@pjfanning
Copy link
Contributor Author

Closing because @frantuma 's has helped me to progress my scala solution

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

2 participants