2525import com .samskivert .mustache .Mustache ;
2626import com .samskivert .mustache .Mustache .Compiler ;
2727import com .samskivert .mustache .Mustache .Lambda ;
28+ import com .fasterxml .jackson .databind .JsonNode ;
2829import io .swagger .v3 .core .util .Json ;
2930import io .swagger .v3 .oas .models .OpenAPI ;
3031import io .swagger .v3 .oas .models .Operation ;
@@ -2559,6 +2560,45 @@ public String toExampleValue(Schema schema) {
25592560 return "null" ;
25602561 }
25612562
2563+ /**
2564+ * Return the list of example values of the property.
2565+ * <p>
2566+ * This method should be overridden in the generator to meet its requirement.
2567+ *
2568+ * @param schema Property schema
2569+ * @return a string list of the example values of the property
2570+ */
2571+ public List <String > toExampleValues (Schema schema ) {
2572+ List <?> schemaExamples = schema .getExamples ();
2573+ if (schemaExamples == null || schemaExamples .isEmpty ()) {
2574+ return null ;
2575+ }
2576+ List <String > examples = schemaExamples .stream ()
2577+ .map (this ::getExampleValueString )
2578+ .collect (Collectors .toList ());
2579+
2580+ return examples .isEmpty () ? null : examples ;
2581+ }
2582+
2583+ /**
2584+ * Return the string presentation of a single example value.
2585+ * <p>
2586+ * For value nodes, {@link JsonNode#asText()} is used to get the string without duplicated quotes.
2587+ *
2588+ * @param example the raw example value coming from the parsed schema
2589+ * @return string presentation of the example value
2590+ */
2591+ protected String getExampleValueString (Object example ) {
2592+ if (example == null ) {
2593+ return "null" ;
2594+ }
2595+ if (example instanceof JsonNode ) {
2596+ JsonNode node = (JsonNode ) example ;
2597+ return node .isValueNode () ? node .asText () : node .toString ();
2598+ }
2599+ return example .toString ();
2600+ }
2601+
25622602 /**
25632603 * Return the default value of the property
25642604 * <p>
@@ -4285,9 +4325,19 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
42854325 try {
42864326 property .example = toExampleValue (p );
42874327 } catch (Exception e ) {
4288- LOGGER .error ("Error in generating `example` for the property {}. Default to ERROR_TO_EXAMPLE_VALUE. Enable debugging for more info." , property .baseName );
4289- LOGGER .debug ("Exception from toExampleValue: {}" , e .getMessage ());
4290- property .example = "ERROR_TO_EXAMPLE_VALUE" ;
4328+ property .example = handleExampleException (property .baseName , e );
4329+ }
4330+
4331+ if (p .getExamples () != null ) {
4332+ List <String > examples = new ArrayList <>();
4333+ for (Object example : p .getExamples ()) {
4334+ try {
4335+ examples .add (getExampleValueString (example ));
4336+ } catch (Exception e ) {
4337+ examples .add (handleExampleException (property .baseName , e ));
4338+ }
4339+ }
4340+ property .examples = examples ;
42914341 }
42924342
42934343 property .jsonSchema = Json .pretty (Json .mapper ().convertValue (p , TreeMap .class ));
@@ -4512,6 +4562,9 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
45124562 if (original .getExample () != null ) {
45134563 property .example = toExampleValue (original );
45144564 }
4565+ if (original .getExamples () != null ) {
4566+ property .examples = toExampleValues (original );
4567+ }
45154568 }
45164569
45174570 // override defaultValue if it's not set and defaultToEmptyContainer is set
@@ -4528,6 +4581,12 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
45284581 return property ;
45294582 }
45304583
4584+ private String handleExampleException (String propertyName , Exception e ) {
4585+ LOGGER .error ("Error in generating `example` for the property {}. Default to ERROR_TO_EXAMPLE_VALUE. Enable debugging for more info." , propertyName );
4586+ LOGGER .debug ("Exception from toExampleValue: {}" , e .getMessage ());
4587+ return "ERROR_TO_EXAMPLE_VALUE" ;
4588+ }
4589+
45314590 /**
45324591 * update container's default to empty container according rules provided by the user.
45334592 *
0 commit comments