26
26
import io .swagger .v3 .oas .models .media .Schema ;
27
27
import io .swagger .v3 .oas .models .parameters .RequestBody ;
28
28
import io .swagger .v3 .oas .models .responses .ApiResponse ;
29
+ import org .apache .commons .lang3 .StringUtils ;
29
30
import org .apache .commons .lang3 .tuple .Pair ;
30
31
import org .openapitools .codegen .*;
31
32
import org .openapitools .codegen .languages .features .BeanValidationFeatures ;
@@ -621,19 +622,25 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
621
622
622
623
for (Map .Entry <String , Schema > e : schemas .entrySet ()) {
623
624
String n = toModelName (e .getKey ());
624
- Schema s = e .getValue ();
625
+ Schema schema = e .getValue ();
625
626
String nOneOf = toModelName (n + "OneOf" );
626
- if (ModelUtils .isComposedSchema (s )) {
627
- addOneOfNameExtension (s , n );
628
- addOneOfInterfaceModel ((ComposedSchema ) s , nOneOf );
629
- } else if (ModelUtils .isArraySchema (s )) {
630
- Schema items = ((ArraySchema ) s ).getItems ();
627
+ if (ModelUtils .isComposedSchema (schema )) {
628
+ List <String > names = new ArrayList <>();
629
+ ComposedSchema composedSchema = (ComposedSchema ) schema ;
630
+ for (Schema oneOfSchema : composedSchema .getOneOf ()) {
631
+ names .add (getSingleSchemaType (oneOfSchema ));
632
+ }
633
+ String name = "OneOf" + String .join ("" , names );
634
+ addOneOfNameExtension (schema , name );
635
+ addOneOfInterfaceModel ((ComposedSchema ) schema , name );
636
+ } else if (ModelUtils .isArraySchema (schema )) {
637
+ Schema items = ((ArraySchema ) schema ).getItems ();
631
638
if (ModelUtils .isComposedSchema (items )) {
632
639
addOneOfNameExtension (items , nOneOf );
633
640
addOneOfInterfaceModel ((ComposedSchema ) items , nOneOf );
634
641
}
635
- } else if (ModelUtils .isMapSchema (s )) {
636
- Schema addProps = ModelUtils .getAdditionalProperties (s );
642
+ } else if (ModelUtils .isMapSchema (schema )) {
643
+ Schema addProps = ModelUtils .getAdditionalProperties (schema );
637
644
if (addProps != null && ModelUtils .isComposedSchema (addProps )) {
638
645
addOneOfNameExtension (addProps , nOneOf );
639
646
addOneOfInterfaceModel ((ComposedSchema ) addProps , nOneOf );
@@ -649,22 +656,41 @@ public void addOneOfNameExtension(Schema s, String name) {
649
656
}
650
657
}
651
658
652
- public void addOneOfInterfaceModel (ComposedSchema cs , String type ) {
659
+ public void addOneOfInterfaceModel (ComposedSchema cs , String name ) {
653
660
CodegenModel cm = new CodegenModel ();
654
661
655
662
for (Schema o : cs .getOneOf ()) {
656
663
// TODO: inline objects
657
664
cm .oneOf .add (toModelName (ModelUtils .getSimpleRef (o .get$ref ())));
658
665
}
659
- cm .name = type ;
660
- cm .classname = type ;
666
+ cm .name = name ;
667
+ cm .classname = name ;
661
668
cm .vendorExtensions .put ("isOneOfInterface" , true );
662
669
cm .discriminator = createDiscriminator ("" , (Schema ) cs );
663
670
cm .interfaceModels = new ArrayList <CodegenModel >();
664
671
672
+ for (Schema schema : cs .getOneOf ()){
673
+ cm .vars .add (fromProperty ("property" , schema ));
674
+ }
665
675
addOneOfInterfaces .add (cm );
666
676
}
667
677
678
+ private String getSingleSchemaType (Schema schema ) {
679
+ Schema unaliasSchema = ModelUtils .unaliasSchema (this .openAPI , schema );
680
+
681
+ if (StringUtils .isNotBlank (unaliasSchema .get$ref ())) { // reference to another definition/schema
682
+ // get the schema/model name from $ref
683
+ String schemaName = ModelUtils .getSimpleRef (unaliasSchema .get$ref ());
684
+ if (StringUtils .isNotEmpty (schemaName )) {
685
+ return getAlias (schemaName );
686
+ } else {
687
+ LOGGER .warn ("Error obtaining the datatype from ref:" + unaliasSchema .get$ref () + ". Default to 'object'" );
688
+ return "object" ;
689
+ }
690
+ }
691
+ return null ;
692
+ }
693
+
668
694
private class OneOfImplementorAdditionalData {
669
695
private String implementorName ;
670
696
private List <String > additionalInterfaces = new ArrayList <String >();
@@ -812,24 +838,28 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
812
838
}
813
839
}
814
840
}
815
-
816
- for (Map .Entry modelsEntry : objs .entrySet ()) {
817
- Map <String , Object > modelsAttrs = (Map <String , Object >) modelsEntry .getValue ();
818
- List <Object > models = (List <Object >) modelsAttrs .get ("models" );
819
- List <Map <String , String >> imports = (List <Map <String , String >>) modelsAttrs .get ("imports" );
820
- for (Object _implmo : models ) {
821
- Map <String , Object > implmo = (Map <String , Object >) _implmo ;
822
- CodegenModel implcm = (CodegenModel ) implmo .get ("model" );
823
- if (additionalDataMap .containsKey (implcm .name )) {
824
- additionalDataMap .get (implcm .name ).addToImplementor (implcm , imports );
825
- }
826
- }
827
- }
828
841
}
829
842
830
843
return objs ;
831
844
}
832
845
846
+ @ Override
847
+ public CodegenParameter fromRequestBody (RequestBody body , Set <String > imports , String bodyParameterName ) {
848
+ CodegenParameter codegenParameter = super .fromRequestBody (body , imports , bodyParameterName );
849
+ Schema schema = ModelUtils .getSchemaFromRequestBody (body );
850
+ CodegenProperty codegenProperty = fromProperty ("property" , schema );
851
+ if (codegenProperty != null && codegenProperty .getComplexType () != null && schema instanceof ComposedSchema ) {
852
+ String codegenModelName = codegenProperty .getComplexType ();
853
+ codegenParameter .baseName = codegenModelName ;
854
+ codegenParameter .paramName = toParamName (codegenParameter .baseName );
855
+ codegenParameter .baseType = codegenParameter .baseName ;
856
+ codegenParameter .dataType = getTypeDeclaration (codegenModelName );
857
+ codegenParameter .description = codegenProperty .getDescription ();
858
+ codegenProperty .setComplexType (codegenModelName );
859
+ }
860
+ return codegenParameter ;
861
+ }
862
+
833
863
@ Override
834
864
public Map <String , Object > postProcessOperationsWithModels (Map <String , Object > objs , List <Object > allModels ) {
835
865
Map <String , Object > operations = (Map <String , Object >) objs .get ("operations" );
0 commit comments