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 ;
@@ -2551,6 +2552,45 @@ public String toExampleValue(Schema schema) {
25512552 return "null" ;
25522553 }
25532554
2555+ /**
2556+ * Return the list of example values of the property.
2557+ * <p>
2558+ * This method should be overridden in the generator to meet its requirement.
2559+ *
2560+ * @param schema Property schema
2561+ * @return a string list of the example values of the property
2562+ */
2563+ public List <String > toExampleValues (Schema schema ) {
2564+ List <?> schemaExamples = schema .getExamples ();
2565+ if (schemaExamples == null || schemaExamples .isEmpty ()) {
2566+ return null ;
2567+ }
2568+ List <String > examples = schemaExamples .stream ()
2569+ .map (this ::getExampleValueString )
2570+ .collect (Collectors .toList ());
2571+
2572+ return examples .isEmpty () ? null : examples ;
2573+ }
2574+
2575+ /**
2576+ * Return the string presentation of a single example value.
2577+ * <p>
2578+ * For value nodes, {@link JsonNode#asText()} is used to get the string without duplicated quotes.
2579+ *
2580+ * @param example the raw example value coming from the parsed schema
2581+ * @return string presentation of the example value
2582+ */
2583+ protected String getExampleValueString (Object example ) {
2584+ if (example == null ) {
2585+ return "null" ;
2586+ }
2587+ if (example instanceof JsonNode ) {
2588+ JsonNode node = (JsonNode ) example ;
2589+ return node .isValueNode () ? node .asText () : node .toString ();
2590+ }
2591+ return example .toString ();
2592+ }
2593+
25542594 /**
25552595 * Return the default value of the property
25562596 * <p>
@@ -4277,9 +4317,19 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
42774317 try {
42784318 property .example = toExampleValue (p );
42794319 } catch (Exception e ) {
4280- LOGGER .error ("Error in generating `example` for the property {}. Default to ERROR_TO_EXAMPLE_VALUE. Enable debugging for more info." , property .baseName );
4281- LOGGER .debug ("Exception from toExampleValue: {}" , e .getMessage ());
4282- property .example = "ERROR_TO_EXAMPLE_VALUE" ;
4320+ property .example = handleExampleException (property .baseName , e );
4321+ }
4322+
4323+ if (p .getExamples () != null ) {
4324+ List <String > examples = new ArrayList <>();
4325+ for (Object example : p .getExamples ()) {
4326+ try {
4327+ examples .add (getExampleValueString (example ));
4328+ } catch (Exception e ) {
4329+ examples .add (handleExampleException (property .baseName , e ));
4330+ }
4331+ }
4332+ property .examples = examples ;
42834333 }
42844334
42854335 property .jsonSchema = Json .pretty (Json .mapper ().convertValue (p , TreeMap .class ));
@@ -4504,6 +4554,9 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
45044554 if (original .getExample () != null ) {
45054555 property .example = toExampleValue (original );
45064556 }
4557+ if (original .getExamples () != null ) {
4558+ property .examples = toExampleValues (original );
4559+ }
45074560 }
45084561
45094562 // override defaultValue if it's not set and defaultToEmptyContainer is set
@@ -4520,6 +4573,12 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
45204573 return property ;
45214574 }
45224575
4576+ private String handleExampleException (String propertyName , Exception e ) {
4577+ LOGGER .error ("Error in generating `example` for the property {}. Default to ERROR_TO_EXAMPLE_VALUE. Enable debugging for more info." , propertyName );
4578+ LOGGER .debug ("Exception from toExampleValue: {}" , e .getMessage ());
4579+ return "ERROR_TO_EXAMPLE_VALUE" ;
4580+ }
4581+
45234582 /**
45244583 * update container's default to empty container according rules provided by the user.
45254584 *
0 commit comments