001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.jsonschema; 014 015import org.apache.juneau.*; 016import org.apache.juneau.json.*; 017import org.apache.juneau.jsonschema.annotation.*; 018import org.apache.juneau.parser.*; 019 020/** 021 * Metadata on bean properties specific to the JSON-Schema pulled from the {@link JsonSchema @JsonSchema} annotation 022 * on the bean property. 023 */ 024public class JsonSchemaBeanPropertyMeta extends BeanPropertyMetaExtended { 025 026 /** 027 * Default instance. 028 */ 029 public static final JsonSchemaBeanPropertyMeta DEFAULT = new JsonSchemaBeanPropertyMeta(); 030 031 private String type, format, description; 032 private Object example; 033 034 /** 035 * Constructor. 036 * 037 * @param bpm The metadata of the bean property of this additional metadata. 038 */ 039 public JsonSchemaBeanPropertyMeta(BeanPropertyMeta bpm) { 040 super(bpm); 041 042 if (bpm.getField() != null) 043 findInfo(bpm.getField().getAnnotation(JsonSchema.class)); 044 if (bpm.getGetter() != null) 045 findInfo(bpm.getGetter().getAnnotation(JsonSchema.class)); 046 if (bpm.getSetter() != null) 047 findInfo(bpm.getSetter().getAnnotation(JsonSchema.class)); 048 } 049 050 private JsonSchemaBeanPropertyMeta() { 051 super(null); 052 this.type = null; 053 this.format = null; 054 this.description = null; 055 this.example = null; 056 } 057 058 private void findInfo(JsonSchema js) { 059 if (js == null) 060 return; 061 if (! js.type().isEmpty()) 062 type = js.type(); 063 if (! js.format().isEmpty()) 064 format = js.format(); 065 if (! js.description().isEmpty()) 066 description = js.description(); 067 if (! js.example().isEmpty()) { 068 try { 069 example = JsonParser.DEFAULT.parse(js.example(), Object.class); 070 } catch (ParseException e) { 071 throw new BeanRuntimeException(e); 072 } 073 } 074 } 075 076 /** 077 * Returns the {@link JsonSchema#type() @JsonSchema(type)} annotation defined on the class. 078 * 079 * @return The value of the annotation, or <jk>null</jk> if not specified. 080 */ 081 protected String getType() { 082 return type; 083 } 084 085 /** 086 * Returns the {@link JsonSchema#format() @JsonSchema(format)} annotation defined on the class. 087 * 088 * @return The value of the annotation, or <jk>null</jk> if not specified. 089 */ 090 protected String getFormat() { 091 return format; 092 } 093 094 /** 095 * Returns the {@link JsonSchema#description() @JsonSchema(description)} annotation defined on the class. 096 * 097 * @return The value of the annotation, or <jk>null</jk> if not specified. 098 */ 099 protected String getDescription() { 100 return description; 101 } 102 103 /** 104 * Returns the {@link JsonSchema#example() @JsonSchema(example)} annotation defined on the class. 105 * 106 * @return The value of the annotation, or <jk>null</jk> if not specified. 107 */ 108 protected Object getExample() { 109 return example; 110 } 111}