title: How to generate SpringBoot models from Swagger yaml with Openapi Generator
body: <p>we are using an API first approach and the API for our services is defined in swagger yamls. We then use Openapi generator to generate the interfaces that have to be implemented by the microservices. Also, the models are generated from the schemas. </p>
  
  <p>This is where the question comes in. In one of the REST endpoints, I consume an XML let it be parsed by Spring-boot into a model and then save it to the database. At the moment I use a model that looks something like this:</p>
  
  <pre><code>@Data
  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  @Document("MyModel")
  public class Model {
  
    @Id 
    @XmlTransient
    private String id;
  
    @XmlAttribute
    private String someAttribute;
  
    @XmlElement("childName")
    private List&lt;OtherModel&gt; children;
  
    @XmlElement("otherChildName")
    private List&lt;OtherModel2&gt; children2; 
  
  }
  </code></pre>
  
  <p>With the other models being pretty similar. I like this because Spring-boot can use this to automatically parse the incoming XML and I can save this model directly to the database and return it as JSON. </p>
  
  <p>The Models that are generated by Openapi Generator are missing the annotation, therefore, cannot be used to parse the XML and cannot be saved to the database. Are there any way in the yaml to tell it to add the <code>@Id</code>, <code>@Document</code> and some <code>@Xml...</code> annotations?</p>
  
  <p>The main reason I'm trying to do this (use autogenerated models form swagger) is because this is a larger architecture where it would be nice if someone changes the API definition in the yaml, and for example adds fields the microservice(s) would also be build again and the models would be updated without manual updating java classes. Another way I thought about was generating the models the way Openapi does, then extending them with own classes and somehow add annotations afterward,... but I cannot think of any way how to do this so that you do not have to change the derived class if a field in the definition changes.</p>
  
  <p>Example:</p>
  
  <pre><code>//class generated by openapigenerator
  public class Model {
    private String field;
    private List&lt;Submodel&gt; children
  
    //autogenerated getters/setters
  }
  
  //model in my code
  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  @Document("MyModel")
  public class MyModel extends Model {
  
    @Id 
    @XmlTransient
    @Getter
    @Setter
    private String id;
  
  }
  </code></pre>
  
  <p>This would be ok but would be missing is the <code>@Xml...</code> annotations on some of the fields. I have seen that it generates <code>@JsonProperty("somename")</code> name just fine in case of json naming, but it seems using the <code>XML: name/attribute</code> properties in the yaml doesn't have any influence on the java code generated (or I'm doing something wrong, trying this: <a href="https://swagger.io/docs/specification/data-models/representing-xml/" rel="nofollow noreferrer">Documentation</a>)</p>
  
  <p>The only thing I could think of would be <code>@Override</code>-ing the getter/setters and annotating them with the attributes, but that would mean I have to touch every microservice that uses a derivate of the model defined by the API. I cannot move the derived class into the API project since the contexts (in this case XML/mongo) could vary much from microservice to microservice. </p>
  
  <p>Any ideas on a good approach for this?</p>
  
  <p>Cheers</p>
  
  <p>----- EDIT ------</p>
  
  <p>After digging in deeper I'm getting more sure that probably the right approach is to add the XML mappings (<code>@XmlAttribute</code>/<code>@XmlElement</code>) annotations in the swagger yaml the way it is mentioned in the documentation, and then extending it in the microservice code to add the database annotations. Basically like the second example only that in the base class the XML annotations would already be generated by swagger codegen. However I have not yet managed to make swagger codegen generate any XML annotation when building the schemas/models.</p>